一、find
find 方法是字符串类型对象带有的方法;它可以从一个字符串(str)中找到另一个字符串(str);
如果找到了,则返回索引;如果没有找到,则返回 -1;


二、index
跟find()方法一样,只不过如果字符串(str)不在 我的字符串(mystr)中会报一个异常.


三、count
在 我的字符串(mystr)里面出现的次数,也就是说这个字符串(str)在里面出现几次。


四、replace
把 我的字符串(mystr) 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.


五、split
以 str 为分隔符切片 mystr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串


六、capitalize
把字符串的第一个字符大写

In [32]: str
Out[32]: 'wo shi zhong guo ren zhong guo ren shi wo'
In [33]: str.capitalize()
Out[33]: 'Wo shi zhong guo ren zhong guo ren shi wo'
七、title
把字符串的每个单词首字母大写

In [34]: str
Out[34]: 'wo shi zhong guo ren zhong guo ren shi wo'
In [35]: str.title()
Out[35]: 'Wo Shi Zhong Guo Ren Zhong Guo Ren Shi Wo'
八、startswith
检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False

In [42]: str
Out[42]: 'wo shi zhong guo ren zhong guo ren shi wo'
In [43]: str.startswith("w")
Out[43]: True
九、endswith
检查字符串是否以obj结束,如果是返回True,否则返回 False.

In [44]: str
Out[44]: 'wo shi zhong guo ren zhong guo ren shi wo'
In [45]: str.endswith("o")
Out[45]: True
十、lower
转换 mystr 中所有大写字符为小写

In [50]: str
Out[50]: 'Wo shi zhong guo ren zhong guo ren shi wo'
In [51]: str.lower()
Out[51]: 'wo shi zhong guo ren zhong guo ren shi wo'
十一、upper
转换 mystr 中的小写字母为大写

In [52]: str
Out[52]: 'Wo shi zhong guo ren zhong guo ren shi wo'
In [53]: str.upper()
Out[53]: 'WO SHI ZHONG GUO REN ZHONG GUO REN SHI WO'
十二、ljust
返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

In [54]: str
Out[54]: 'Wo shi zhong guo ren zhong guo ren shi wo'
In [55]: str.ljust(50,"#")
Out[55]: 'Wo shi zhong guo ren zhong guo ren shi wo#########'
In [56]: str.ljust(50)
Out[56]: 'Wo shi zhong guo ren zhong guo ren shi wo '
十三、rjust
返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

In [57]: str
Out[57]: 'Wo shi zhong guo ren zhong guo ren shi wo'
In [58]: str.rjust(50,"#")
Out[58]: '#########Wo shi zhong guo ren zhong guo ren shi wo'
In [59]: str.rjust(50)
Out[59]: ' Wo shi zhong guo ren zhong guo ren shi wo'
十四、center
返回一个原字符串居中,并使用空格填充至长度 width 的新字符串

In [60]: str
Out[60]: 'Wo shi zhong guo ren zhong guo ren shi wo'
In [61]: str.center(50,"#")
Out[61]: '####Wo shi zhong guo ren zhong guo ren shi wo#####'
In [62]: str.center(50)
Out[62]: ' Wo shi zhong guo ren zhong guo ren shi wo '
十五、lstrip
删除 mystr 左边的空白字符

In [64]: str
Out[64]: ' Wo shi zhong guo ren zhong guo ren shi wo'
In [65]: str.lstrip()
Out[65]: 'Wo shi zhong guo ren zhong guo ren shi wo'
十六、rstrip
删除 mystr 字符串末尾的空白字符

In [67]: str
Out[67]: 'Wo shi zhong guo ren zhong guo ren shi wo '
In [68]: str.rstrip()
Out[68]: 'Wo shi zhong guo ren zhong guo ren shi wo'
十七、strip
删除mystr字符串两端的空白字符

In [70]: str
Out[70]: ' Wo shi zhong guo ren zhong guo ren shi wo '
In [71]: str.strip()
Out[71]: 'Wo shi zhong guo ren zhong guo ren shi wo'
十八、partition
把mystr以str分割成三部分,str前,str和str后

In [76]: str = " Wo shi zhong guo ren zhong guo ren shi wo "
In [77]: str
Out[77]: ' Wo shi zhong guo ren zhong guo ren shi wo '
In [78]: str.partition("ren")
Out[78]: (' Wo shi zhong guo ', 'ren', ' zhong guo ren shi wo ')
十九、rpartition
类似于 partition()函数,不过是从右边开始.

In [79]: str
Out[79]: ' Wo shi zhong guo ren zhong guo ren shi wo '
In [80]: str.rpartition("ren")
Out[80]: (' Wo shi zhong guo ren zhong guo ', 'ren', ' shi wo ')
二十、splitlines
按照行分隔,返回一个包含各行作为元素的列表

In [81]: str = "Wo shi zhong guo ren \nzhong\n guo ren shi wo"
In [82]: str.splitlines()
Out[82]: ['Wo shi zhong guo ren ', 'zhong', ' guo ren shi wo']
二十一、isalpha
如果 mystr 所有字符都是字母 则返回 True,否则返回 False

In [85]: str = "Woshizhongguoren"
In [86]: str.isalpha()
Out[86]: True
二十二、isdigit
如果 mystr 只包含数字则返回 True 否则返回 False.

In [87]: str = "123"
In [88]: str.isdigit()
Out[88]: True
二十三、isalnum
如果 mystr 所有字符都是字母或数字则返回 True,否则返回 False

In [91]: str
Out[91]: '123asd'
In [92]: str.isalnum()
Out[92]: True
In [93]: str = "123"
In [94]: str.isalnum()
Out[94]: True
In [95]: str = "asd"
In [96]: str.isalnum()
Out[96]: True
二十四、isspace
如果 mystr 中只包含空格,则返回 True,否则返回 False.

In [1]: str = "asd "
In [2]: str.isspace()
Out[2]: False
In [3]: str = " "
In [4]: str.isspace()
Out[4]: True
二十五、joinmystr
中每个字符后面插入str,构造出一个新的字符串

In [5]: str = "woshizhongguoren"
In [6]: str1 = "-"
In [7]: str1.join(str)
Out[7]: 'w-o-s-h-i-z-h-o-n-g-g-u-o-r-e-n'
In [8]: li = ["a","s","d","f"]
In [9]: str1.join(li)
Out[9]: 'a-s-d-f'
网友评论