python函数学习之字符串函数

1.capitalize()返回原始字符串的首字母大写版本,其他任何大写字母转换成小写

>>> ls='hello WorLd'
>>> ls.capitalize()
'Hello world'

2.center(width)返回width的一个字符串,并让原始字符串在其中,两边用空格填充

>>> ls='hello'
>>> ls.center(12)
' hello '

3.count(substring,start,end)返回substring在原始字符串中的出现次数,可以指定起止位置

4.encode(encoding,errors) 返回一个编码的字符串,error标识要使用的错误处理类型,默认'strict'

5.endswith(substring,start,end)如果字符串以substring结束,返回1,否则返回0

   startswith(substring,start,end)如果字符串以substring开始,返回1,否则返回0

6.expandtabs(tabsize=8)返回一个新字符串,其中所有制表符都被替换成空格,tabsize可选参数用于指定用于替代一个制表符的空格字符数,默认8

7.find(substr,start,end)返回substr在字符串中出现的最低索引位置,不包括的话返回-1

8.index(substring,start,end) 与find类似,没有发现substring引发valueError异常

9.isalnum() 如果字符串治保会字母/数字字符,返回1,否则返回0

>>> ls='232afsa'
>>> ls.isalnum()
True

10.isalpha()如果字符串只包含字母,返回1,否则返回0

11.isdigit() 如果字符串只包含数字,返回1,否则返回0

12.islower()如果所有字母字符都是小写,返回1,否则返回0

>>> ls='23#aaffsad'
>>> ls.islower()
True
>>>

13.isspace()所有字符串都为空白字符,返回1,否则返回0

14.istitle() 如果字符串中个单词都是首字母大写,返回1,否则返回0

15.isupper() 如果所有字母都是大写字母,返回1,否则返回0

16.join(sequence)返回一个字符串,连接sequence中的所有字符串,源字符串作为分隔

17.ljust(width)返回一个字符串,原始字符串在宽度为width的一个空白字符串中左对齐

     rjust(width)返回一个字符串,原始字符串在宽度为width的一个空白字符串中右对齐

18.lower()  大写变小写

19.lstrip()删除开头的所有空白字符,包括

     rstrip()删除末尾的所有空白字符,包括

20.replace(old,new,maximum)maximum表示几次替换

21.rfind(substr,start,end)返回substring在字符串中的最高索引位置,不包括则返回-1

22.rindex(substr,start,end)类似rfind,不包括返回ValueError异常

23.splitlines(keepbreaks)换行符分割字符串,得到列表,keepbreaks=1则保留换行符

24.strip() 删除开头结尾的空白字符,包括

25.swapcase() 将所有大写转换为小写,将所有小写转换为大写

26.title()返回新字符串,每个单词首字母大写,单词其他字母小写

27.translate(table,delete) 首先删除可选参数delete中的所有字符,然后将原始字符串中的每个字符串c换成table[ord(c)]值

28.upper()小写换大写

原文地址:https://www.cnblogs.com/BetterThanEver_Victor/p/10993553.html