- 字符串类型
split通过指定分隔符分割字符串
例子:
name = "huang zhong"
print(name.split())
strip移除字符串头部和尾部的字符,默认空格, lstrip移除左侧字符,rstrip移除右侧字符
例子:
name = " alex "
print(name.strip())
capitalize首字母转换成大写
例子:
name = "alex"
print(name.capitalize())
casefold将所有大写变小写,可以转换多门语言
例子:
name = "AleX"
v = name.casefold()
print(v)
lower将所有大写变小写,只能转换英语
例子:
name = "AleX"
v = name.casefold()
print(v)
center文本居中,空白处用其他字符填充,默认空白字符填充
例子:
name = "alex"
v = name.center(20, ‘*‘)
print(v)
count子字符串在父字符串中出现的次数
例子:
name = "cndklsdnrfknsv"
num = name.count("d")
num1 = name.count("d", 5) #从第5个索引开始计算
num2 = name.count("d", 5, 9) #计算从第5到第9之间d出现的次数
print(num)
startswith是否以某个字符开始,是则返回True,否则返回False
例子:
name = "alex"
v = name.startswith("al")
print(v)
endswith是否以某个字符开始,是则返回True,否则返回False
例子:
name = "alex"
v = name.startswith("ex")
print(v)
expandtabs找到制表符并替换
例子:
name = "nsdl\tajnfcks\tsns"
v = name.expandtabs(20) #替换成20个字符,不足的补空白
print(v)
find找到子序列的索引位置,没找到返回-1
例子:
name = "alex"
v = name.find("o") #-1
print(v)
index找到子序列的索引位置,没找到报错
例子:
name = "alex"
v = name.index("a")
print(v)
format字符串格式化
例子:
name = v = name.format(, , ) (v) name = v = name.format(=,=, =) (v)
isalnum是否是数字、字符、汉字
例子:
name = v = name.isalnum() (v) num = "二" v1 = num.isdecimal() # ‘123‘ v2 = num.isdigit() # ‘123‘ v3 = num.isnumeric() # ‘123‘,‘二‘