python3字符串的常见操作
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]: -12、index和find一樣只不過,str不在mystr中會報一個異常
In [8]: mystr.index('itcast') Out[8]: 12In [9]: mystr.index('heihei') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-9-2cd231cae32a> in <module>() ----> 1 mystr.index('heihei')ValueError: substring not found3、rfind、rindex從右往左找
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' In [10]: mystr.rfind('itcast') Out[10]: 27In [12]: mystr.rindex('itcast') Out[12]: 274、count返回str在start和end之間,在mystr里出現的次數
In [13]: mystr.count('itcast') Out[13]: 25、replace把mystr中的str1替換成str2,如果count指定則不超過count次數
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' In [27]: mystr.startswith('hello') Out[27]: TrueIn [28]: mystr.startswith('wee') Out[28]: False10、endwith檢查字符串是不是以“xxx”結尾,如果是返回True,否則返回False
In [29]: mystr.endswith('cast') Out[29]: TrueIn [30]: mystr.endswith('amst') Out[30]: False11、lower轉換所有大寫字母為小寫
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' In [32]: mystr='HELLO WORLD ITCAST AND HAHAITCAST'In [33]: mystr.lower() Out[33]: 'hello world itcast and hahaitcast'12、upper轉換所有小寫字母為大寫
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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刪除左邊的空白字符
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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按照行分隔,返回一個包含各行作為元素的列表
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' 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]: FalseIn [57]: lyc='sadsadsasa'In [58]: lyc.isalpha() Out[58]: True23、isdigit如果 mystr 只包含數字則返回 True 否則返回 False.
In [61]: lyc='1213213'In [62]: lyc.isd lyc.isdecimal lyc.isdigitIn [62]: lyc.isdigit() Out[62]: TrueIn [63]: lyc='sada123'In [64]: lyc.isd lyc.isdecimal lyc.isdigitIn [64]: lyc.isdigit() Out[64]: False總結
以上是生活随笔為你收集整理的python3字符串的常见操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [python]函数默认参数顺序问题
- 下一篇: Python之dict(或对象)与jso