capitalize() 首字母大写
a='someword' b=a.capitalize() print(b) —>Someword
casefold()&lower() 所有字母变小写,casefold可将未知字符便小写
a='someWORD' b=a.casefold() print(b) c=a.lower() print(c) —>someword —>someword
center(width,fillchar=None) 设置宽度,并将内容居中,空白未知填充,一个字符
a='someword' b=a.center(30,'*') print(b)
count(sub,start=None,end=None) 去字符串中寻找,寻找子序列的出现次数,可指定起止点
a='somewordsomeword' b=a.count(‘or') print(b) —>2
startswith(suffix,start=None,end=None)&endswith(suffix,start=None,end=None) 是否以XX开始/结束,可指定起止点
a='somewordsomeword' b=a.startswith(‘sa') c=a.endswith(‘ord') print(b) print(c) —>False —>True
find(sub,start=None,end=None) 寻找指定字符或字符串,并返回第一个位置,找不到返回-1,可指定起止点
a='somewordsomeword' b=a.find(‘me') print(b) —>2
format() 格式化,将一个字符串中的占位符替换为指定的值
test='I am {name},age {a}' v=test.format(name='alex',a=19) print(v) —>i am alex,age 19
format_map() 格式化,传入的值
test='iam{name},age{a}' v=test.format_map({“name”:'alex',”a”:19}) print(v) —>i am alex,age 19
isalnum() 字符串中是否只包含字母和数字
a='asdfs123*' b=a.isalnum() print(b) —>False
expandtabs(tabsize=number) 将字符串以number分割,并将tab补入
a='asdfs123/t523fgbdf' b=a.expandtabs(5) print(b) —>asdfs123 523fgbdf
isalpha() 字符串中是只包含字母
a='asdfsfgbdf' b=a.isalpha() print(b) —>True
isdecimal()&isdigit()&isnumeric() 字符串中是只包含数字,isdigit更为强大,isnumeric还可识别中文
a='132132②二' b=a.isdecimal() c=a.isdigit() d=a.isnumeric() print(b) print(c) print(d) —>False —>False —>True
isprintable() 是否存在不可显示的字符如换行符
a='sdfgdfg/t' b=a.isprintable() print(b) —>False
isspace() 判断是否全部为空格
a='dsvsdv' b=a.isspace() print(b) —>False
istitle()&title() 判断是否为标题,即首字母大写&变为标题
a='follow uncased characters and lowercase characters only cased ones' b=a.istitle() print(b) c=a.title() print(c) —>False —>Follow Uncased Characters And Lowercase Characters Only Cased Ones
新闻热点
疑难解答