find,join,lower,replace,split,strip,translatepython
In [90]: s='apple,peach,banana,peach,pear' In [91]: s.find('peach') Out[91]: 6 In [92]: s.find('peach',7) Out[92]: 19 In [93]: s.find('peach',7,18) Out[93]: -1 In [94]: s.find('p') Out[94]: 1 In [95]: s.rfind('p') Out[95]: 25 In [96]: s.index('ban') Out[96]: 12 In [97]: s.index('beauty') Traceback (most recent call last): File "<ipython-input-97-cbc84c2c1291>", line 1, in <module> s.index('beauty') ValueError: substring not found In [98]: s.count('p') Out[98]: 5 In [99]: s.conut('beauty') Traceback (most recent call last): File "<ipython-input-99-097b6137e8f2>", line 1, in <module> s.conut('beauty') AttributeError: 'str' object has no attribute 'conut'
In [1]: t='2019-10-27' In [2]: t.split('-') Out[2]: ['2019', '10', '27'] 分隔後的結果是列表 In [7]: list(map(int,t.split('-'))) Out[7]: [2019, 10, 27] In [8]: s='apple,peach,banana,pear' In [9]: s.split(',') Out[9]: ['apple', 'peach', 'banana', 'pear'] In [10]: q='apple\npeach\tbanana\fpear orange' 不指定分隔符,則字符串中的任何空白符號(空格,換行符,製表符等)都被認爲是分隔符 In [11]: q.split() Out[11]: ['apple', 'peach', 'banana', 'pear', 'orange'] In [14]: s.split(',',maxsplit=1) Out[14]: ['apple', 'peach,banana,pear'] 指定最大分隔次數
In [15]: s='apple peace banana pear'git
In [16]: s.split()api
Out[16]: ['apple', 'peace', 'banana', 'pear']app
In [17]: s1='apple \npeace\t\tbanana\f\npear' 不指定分隔符分化,認爲多個連續分隔符是一個分隔符spa
In [18]: s1.split()code
Out[18]: ['apple', 'peace', 'banana', 'pear']blog
In [19]: s='apple,,,peach,,,banana,,pear' ip
In [22]: s2='apple,,,peach,,,banana,,pear'字符串
In [23]: s2.split(',')input
Out[23]: ['apple', '', '', 'peach', '', '', 'banana', '', 'pear'] 指定分隔符的化,每一個都是分隔符
In [25]: l=['abc','qwe','asd','bhy'] join()方法將列表中多個字符串進行鏈接,並在相鄰兩個字符串之間插入指定字符,返回新字符串 In [26]: ','.join(l) Out[26]: 'abc,qwe,asd,bhy' In [27]: '**'.join(l) Out[27]: 'abc**qwe**asd**bhy' In [29]: ''.join(l) Out[29]: 'abcqweasdbhy'
In [30]: m='how old are you' In [31]: m.upper() Out[31]: 'HOW OLD ARE YOU' In [32]: m.title() Out[32]: 'How Old Are You' In [33]: m.capitalize() Out[33]: 'How old are you' In [34]: M='HOW OLD ARE YOU' In [35]: M.lower() Out[35]: 'how old are you' In [37]: Mm='HOW OLD are You' In [38]: Mm.swapcase() Out[38]: 'how old ARE yOU'
In [39]: Mm='HOW OLD are You' In [40]: Mm.replace('OLD','mmmm') 查找而且替換,每次只能替換一個 Out[40]: 'HOW mmmm are You'
In [41]: Mm=' HOW OLD are You ' In [42]: Mm.strip() Out[42]: 'HOW OLD are You' In [43]: Mm.rstrip() Out[43]: ' HOW OLD are You' In [44]: asd='aasssddmmmmmmmasd' In [45]: asd.strip('asd') Out[45]: 'mmmmmmm'
In [49]: Mm='HOW OLD are You' In [50]: Mm.startswith('HO') Out[50]: True In [51]: Mm.endswith('You') Out[51]: True
In [52]: '123abc'.isalnum() 字母或數字 Out[52]: True In [53]: '123'.isdigit() 數字 Out[53]: True In [54]: "abc".isalpha() 字母 Out[54]: True
In [55]: 'high school'.center(20) Out[55]: ' high school ' In [56]: 'high school'.center(20,'%') Out[56]: '%%%%high school%%%%%' In [57]: 'high school'.ljust(20) Out[57]: 'high school ' In [58]: 'high school'.rjust(20) Out[58]: ' high school'