1、常用查找方法len() 字符串长度
- a.startswith() 以指定字符串开头
- a.endswith() 以指定字符串结尾
- a.find() 第一次出现指定字符串的位置
- a.rfind() 最后一次出现指定字符串的位置
- a.count() 指定字符串出现了几次
- a.isalnum() 所有字符全是字母或数字
>>> a="how are you"
>>> len(a)
11
>>> a.startswith("how")
True
>>> a.endswith("you")
True
2、去除首尾信息
- strip()去除字符串首尾指定信息。
- lstrip()去除字符串左边指定信息
- rstrip()去除字符串右边指定信息。
>>> str="*a*b*c*"
>>> str.strip("*")
'a*b*c'
>>> str.lstrip("*")
'a*b*c*'
>>> str.rstrip("*")
'*a*b*c'
3、大小写转换
- a.capitalize() 产生新的字符串,首字母大写
- a.title() 产生新的字符串,每个单词都首字母大写
- a.upper() 产生新的字符串,所有字符全转成大写
- a.lower() 产生新的字符串,所有字符全转成小写
- a.swapcase() 产生新的,所有字母大小写转换
4、判断
- isalnum() 是否为字母或数字
- isalpha() 检测字符串是否只由字母组成(含汉字)。
- isdigit() 检测字符串是否只由数字组成。
- isspace() 检测是否为空白符
- isupper() 是否为大写字母
- islower() 是否为小写字母
5、排版
- center()
- ljust()
- rjust()
6、字符串的格式化
format()基本用法:通过{索引}/{参数名},直接映射参数值,实现对字符串的格式化
>>> a="my name is {0},my son is {1}, {1} {2} 岁了"
>>> a.format("zxq","wzm",4)
'my name is zxq,my son is wzm, wzm 4 岁了'