当前位置: 移动技术网 > IT编程>脚本编程>Python > python中字符串的常见操作

python中字符串的常见操作

2019年12月12日  | 移动技术网IT编程  | 我要评论

无邪赋,杨奕佳,南斗六星

1、find检测str是否包含在mystr,如果是返回开始的索引值,否则返回-1

in [5]: mystr='hello world itcast and hahaitcast'
in [6]: mystr.find('world')
out[6]: 6
in [7]: mystr.find('heihei')
out[7]: -1

2、index和find一样只不过,str不在mystr中会报一个异常

in [8]: mystr.index('itcast')
out[8]: 12
 
in [9]: mystr.index('heihei')
---------------------------------------------------------------------------
valueerror traceback (most recent call last)
<ipython-input-9-2cd231cae32a> in <module>()
----> 1 mystr.index('heihei')
 
valueerror: substring not found

3、rfind、rindex从右往左找

in [10]: mystr.rfind('itcast')
out[10]: 27
 
in [12]: mystr.rindex('itcast')
out[12]: 27

4、count返回str在start和end之间,在mystr里出现的次数

in [13]: mystr.count('itcast')
out[13]: 2

5、replace把mystr中的str1替换成str2,如果count指定则不超过count次数

in [18]: mystr
out[18]: 'hello world itcast and hahaitcast'
 
in [19]: mystr.replace('world','world')
out[19]: 'hello world itcast and hahaitcast'
 
in [21]: mystr.replace('itcast','xxxxx',1)
out[21]: 'hello world xxxxx and hahaitcast'
 
in [22]: mystr.replace('itcast','xxxxx',2)
out[22]: 'hello world xxxxx and hahaxxxxx'

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

in [23]: mystr.split(' ')
out[23]: ['hello', 'world', 'itcast', 'and', 'hahaitcast']
 
in [24]: mystr.split(' ',2)
out[24]: ['hello', 'world', 'itcast and hahaitcast']

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

in [25]: mystr.capitalize()
out[25]: 'hello world itcast and hahaitcast'

8、title把字符串的每个单词首字母大写

n [26]: mystr.title()
out[26]: 'hello world itcast and hahaitcast'

9、startwith检查字符串是不是以“xxx”开头,如果是返回true,否则返回false

in [27]: mystr.startswith('hello')
out[27]: true
 
in [28]: mystr.startswith('wee')
out[28]: false

10、endwith检查字符串是不是以“xxx”结尾,如果是返回true,否则返回false

in [29]: mystr.endswith('cast')
out[29]: true
 
in [30]: mystr.endswith('amst')
out[30]: false

11、lower转换所有大写字母为小写

in [32]: mystr='hello world itcast and hahaitcast'
 
in [33]: mystr.lower()
out[33]: 'hello world itcast and hahaitcast'

ps:遇到问题没人解答?需要python学习资料?可以加点击下方链接自行获取
note.youdao.com/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76

12、upper转换所有小写字母为大写

in [34]: mystr='hello world itcast and hahaitcast'
 
in [35]: mystr.upper()
out[35]: 'hello world itcast and hahaitcast'

13、ljust返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

[36]: mystr.ljust(50)
out[36]: 'hello world itcast and hahaitcast '

14、center返回一个原字符串居中,并使用空格填充至长度 width 的新字符串

in [37]: mystr.center(50)
out[37]: ' hello world itcast and hahaitcast '

15、rjust返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

in [38]: mystr.rjust(50)
out[38]: ' hello world itcast and hahaitcast'

16、lstrip删除左边的空白字符

in [39]: ly=' hello world '
 
in [40]: ly.lstrip()
out[40]: 'hello world '

17、rstrip删除右边的空白字符

in [41]: ly.rstrip()
out[41]: ' hello world'

18、strip删除字符串两端的空白字符

in [42]: ly.strip()
out[42]: 'hello world'

19、partition把mystr以str分割成三部分,str前,str和str后

in [43]: mystr.partition('itcast')
out[43]: ('hello world ', 'itcast', ' and hahaitcast')

20、rpartition类似于 partition()函数,不过是从右边开始.

in [44]: mystr.rpartition('itcast')
out[44]: ('hello world itcast and haha', 'itcast', '')

21、splitlines按照行分隔,返回一个包含各行作为元素的列表

in [51]: lyc='sadsa\nfasfs\nsadasd\nsad\nsd32dadas'
 
in [52]: lyc.splitlines()
out[52]: ['sadsa', 'fasfs', 'sadasd', 'sad', 'sd32dadas']

22、isalpha如果 mystr 所有字符都是字母 则返回 true,否则返回 false

in [55]: lyc='sadsa\nfasfs\nsadasd\nsad\nsd32dadas'
 
in [56]: lyc.isalpha()
out[56]: false
 
in [57]: lyc='sadsadsasa'
 
in [58]: lyc.isalpha()
out[58]: true

23、isdigit如果 mystr 只包含数字则返回 true 否则返回 false.

in [61]: lyc='1213213'
 
in [62]: lyc.isd
lyc.isdecimal lyc.isdigit
 
in [62]: lyc.isdigit()
out[62]: true
 
in [63]: lyc='sada123'
 
in [64]: lyc.isd
lyc.isdecimal lyc.isdigit
 
in [64]: lyc.isdigit()
out[64]: false

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网