python 中常用的字符串处理函数01
001、capitalize:将字符串的第一个字符改为大写,其他字符全部改为小写
>>> a = "abc" >>> b = "ABC" >>> c = "aBc" >>> a.capitalize() ## 将字符串的第一个字符改为大写,其他字符全部改为小写 'Abc' >>> b.capitalize() 'Abc' >>> c.capitalize() 'Abc'
002、casefold:将字符串的所有字符修改为小写
>>> a = "abc" >>> b = "ABC" >>> c = "aBc" >>> a.casefold() ## 将字符串的所有字符修改为小写 'abc' >>> b.casefold() 'abc' >>> c.casefold() 'abc'
003、center(width[,fillchar]): 指定字符串的宽度及填充字符
>>> a = "abc" >>> a.center(1) 'abc' >>> a.center(10) ' abc ' >>> a.center(10, "0") ## 指定字符宽度,并指定填充字符 '000abc0000' >>> a.center(10, "x") 'xxxabcxxxx'
004、count(sub[,stat[,end]]): 统计字符出现的次数
>>> a = "abcabcaaax" >>> a 'abcabcaaax' >>> a.count("a") ## 统计字符出现的次数 5 >>> a.count("b") 2 >>> a.count("x") 1 >>> a.count("a", 0, 3) ## 指定统计范围 1 >>> a.count("a", 0, 5) 2 >>> a.count("a", 1, 5) 1