本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
python中字符串的常见操作
奔跑的男人 · 553浏览 · 发布于2019-12-12 +关注

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]: 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


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1320浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 903浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 964浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 959浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1084浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
分类专栏
小鸟云服务器
扫码进入手机网页