find,join,lower,replace,split,strip,translate
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 peach bananafpear 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'
In [16]: s.split()
Out[16]: ['apple', 'peace', 'banana', 'pear']
In [17]: s1='apple peace bananaf pear' 不指定分隔符分化,认为多个连续分隔符是一个分隔符
In [18]: s1.split()
Out[18]: ['apple', 'peace', 'banana', 'pear']
In [19]: s='apple,,,peach,,,banana,,pear'
In [22]: s2='apple,,,peach,,,banana,,pear'
In [23]: s2.split(',')
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'