如:str="ma zhang feng yue cui"字符串有以下几种常见的操作
In [5]: str = "ma zhang feng yue cui"
In [6]: str.find("ma")
Out[6]: 0
In [7]: str.find("cui")
Out[7]: 18
In [8]: str.find("x")
Out[8]: -1
- find查找在str中是否包括某一个字符,包含的有就返回次字符的索引值,否则返回-1
In [9]: str = "ma zhang feng yue cui"
In [10]: str.index("z")
Out[10]: 3
In [11]: str.index("x")
------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-356a90d50111> in <module>()
----> 1 str.index("x")
ValueError: substring not found
- index和find方法一样,但如果str中不包含此字符,则会报一个错误
In [12]: str = "ma zhang feng yue cui"
In [13]: str.count("a")
Out[13]: 2
- count是计算str中某个字符出现过多少次
In [14]: str = "ma zhang feng yue cui"
In [15]: str.replace("ma","yue")
Out[15]: 'yue zhang feng yue cui'
- replace更换str中某个字符为另一个字符
In [16]: str = "ma zhang feng yue cui"
In [17]: str.split(" ")
Out[17]: ['ma', 'zhang', 'feng', 'yue', 'cui']
In [18]: str.split(" ",2)
Out[18]: ['ma', 'zhang', 'feng yue cui']
- split 为分隔str内的字符,如果定的有固定值,则之分隔固定值相应的字符
In [19]: str = "ma zhang feng yue cui"
In [20]: str.capitalize()
Out[20]: 'Ma zhang feng yue cui'
In [21]: str.title()
Out[21]: 'Ma Zhang Feng Yue Cui'
In [22]: str.lower()
Out[22]: 'ma zhang feng yue cui'
In [23]: str.upper()
Out[23]: 'MA ZHANG FENG YUE CUI'
- capitalize 把str中的一个字换为大写
- title 把字符串的每个单词首字母大写
- lower 把字符串中所有大写更换为小写
- upper 把str中所有小写更换为大写
In [25]: str = "ma zhang feng yue cui"
In [26]: str.startswith("m")
Out[26]: True
In [27]: str.startswith("a")
Out[27]: False
In [28]: str.endswith("i")
Out[28]: True
In [29]: str.endswith("u")
Out[29]: False
- startswith 判断str中是否以某个字符开头,如果是返回true,不是返回false
- endswith 判断str中是否以某个字符结尾,如果是返回true,不是返回false
In [32]: str = "ma zhang feng yue cui"
In [33]: str.ljust(40,"*")
Out[33]: 'ma zhang feng yue cui*******************'
In [34]: str.rjust(40,"*")
Out[34]: '*******************ma zhang feng yue cui'
In [35]: str.center(40,"*")
Out[35]: '*********ma zhang feng yue cui**********'
- ljust 可以返回一个原字符串左对齐,并且使用*号填充至固定值的新字符串
- rjust 可以返回一个原字符串右对齐,并且使用*号填充至固定值的新字符串
- center 可以返回一个原字符串中间对齐,并且两端用号平均填充至固定值的新字符串
&“”为设置符号,可以根据自己需要来更换
In [36]: str = " ma zhang feng yue cui"
In [37]: str.lstrip()
Out[37]: 'ma zhang feng yue cui'
In [38]: str = "ma zhang feng yue cui "
In [39]: str.rstrip()
Out[39]: 'ma zhang feng yue cui'
In [40]: str = " ma zhang feng yue cui "
In [41]: str.strip()
Out[41]: 'ma zhang feng yue cui'
- lstrip 删除str左边的空白字符
- rstrip 删除str右边的空白字符
- strip 删除str两端的空白字符
In [42]: str = "ma zhang feng yue cui"
In [43]: str.partition("feng")
Out[43]: ('ma zhang ', 'feng', ' yue cui')
In [44]: str = "ma zhang feng yue cui"
In [45]: str.rpartition("feng")
Out[45]: ('ma zhang ', 'feng', ' yue cui')
- partition 定义一个str中的字符,把str分隔为定义字符前,定义字符,定义字符后三部分
- rpartiton 与partition一样,只不过从后开始分隔
In [48]: str = "ma zhang\n feng yue cui"
In [49]: str.splitlines()
Out[49]: ['ma zhang', ' feng yue cui']
- splitlines 从str中按行分隔
In [54]: str="afdgsdfhjgsfg"
In [55]: str.isalpha()
Out[55]: True
In [56]: str="21321564"
In [57]: str.isdigit()
Out[57]: True
In [58]: str="123abc"
In [59]: str.isalnum()
Out[59]: True
- isalpha 如果str中全是字母,则返回true,否则返回false
- isdigit 如果str中全是数字,则返回true,否则返回false
- isalnum 如果str中是数字或者字母两种,则返回true,否则返回false
In [64]: str = " "
In [65]: str.isspace()
Out[65]: True
In [66]: str="dadas "
In [67]: str.isspace()
Out[67]: False
- isspace 如果str中只包含空格字符,则返回true,否则返回false
In [68]: str="xueziweixiao"
In [69]: mystr="*"
In [70]: mystr.join(str)
Out[70]: 'x*u*e*z*i*w*e*i*x*i*a*o'
- join 定义新的字符串格式,在str中插入,反馈一个新的字符串