python3显示字符命令_python3字符串的常见操作

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

In [5]: mystr='hello world itcast and hahaitcast'In [6]: mystr.find('world')

Out[6]: 6In [7]: mystr.find('heihei')

Out[7]: -1

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

In [8]: mystr.index('itcast')

Out[8]: 12In [9]: mystr.index('heihei')---------------------------------------------------------------------------ValueError Traceback (most recent call last) in ()----> 1 mystr.index('heihei')

ValueError: substringnot found

3、rfind、rindex从右往左找

In [10]: mystr.rfind('itcast')

Out[10]: 27In [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'

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值