字符串是python中的数据类型,python不支持单个字符,str='a'表示的是只有一个字符的字符串
字符串的内置方法
capitalize():将字符串是一个字母变为大写
str="miaomiao"
print(str.capitalize())
输出:Miaomiao
casefold():将字符串都变成小写
str="MIAOmiao"
print(str.casefold())
输出:miaomiao
center(width):将字符串居中,左右间隔width
str="MIAOmiao"
print(str.center(40))
输出:' MIAOmiao '
count(sub[,star[,end]]):寻找字符串中sub出现的次数,star,end是范围,可选
str="miaomiao"
print(str.count('ao'))
输出:2
endswith(sub[,star[,end]]):检查字符串是否以sub结尾
str="miaomiao"
print(str.endswith('ao'))
输出:True
expendtabs(tabsize):把字符串中的\t替换为空格,tabsize默认为8个
str="miao\tmi\tao"
print(str.expandtabs())
输出:miao mi ao
find(sub[,star[,end]]):检查sub是否在字符串中,如果有返回索引值,如果没有返回-1
str="miaomiao"
print(str.find('ao'))
输出:2
istitle():是否符合标题化,开头第一个字母为大写
str1="miaoMiao"
str2="Miaomiao"
print("str1:",str1.istitle(),"str2:",str2.istitle())
输出:str1: False str2: True
join():以字符串作为分隔符隔开
str="miaoMiao"
print(str.join('xxx'))
输出:xmiaoMiaoxmiaoMiaox
split():分片
str="miao Miao"
print(str.split())
['miao', 'Miao']
........