字符串不僅支持所有通用序列操作蟹瘾,還實現(xiàn)了很多附件方法弛秋。
我會以『字符串方法』為標題,分幾篇筆記逐一介紹這些方法尾序。
我會在這倉庫中持續(xù)更新筆記:https://github.com/orca-j35/python_notes
join
?? str.join(iterable)
Return a string which is the concatenation of the strings in iterable. A TypeError
will be raised if there are any non-string values in iterable, including bytes
objects. The separator between elements is the string providing this method.
# 將iterable中字符串進行連接亥啦,并以調(diào)用該方法的字符串作為分隔符
>>> '-'.join(['ab','cd','ef'])
'ab-cd-ef'
>>> '-'.join(['ab'])
'ab'
>>> '-'.join([])
''
>>> '/'.join(dict(name='joy',age=3))
'name/age'
# 如果iterable中包含非字符串對象,則會拋出TypeError異常
# bytes對象同樣會引發(fā)TypeError異常
partition&rpartition
?? str.partition(sep)
Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.
# 該方法會將字符分拆為三個部分
# 從字符串低位索引開始辉阶,在一次遇到sep時對字符串進行分拆先壕,會將字符串分拆為3個字符串:
# sep之前的字符構成第一個字符串,sep構成第二個字符串谆甜,sep之后的字符構成第三個字符串
>>> 'abcdabcd'.partition('cd')
('ab', 'cd', 'abcd')
>>> 'abcdabcd'.partition('ab')
('', 'ab', 'cdabcd')
>>> 'abcd'.partition('cd')
('ab', 'cd', '')
# 如果字符串中沒有sep垃僚,也會返回三個元組:
# 原字符串構成第一個字符串,后兩個字符串均為空
>>> 'abcdabcd'.partition('ef')
('abcdabcd', '', '')
?? str.rpartition(sep)
Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.
# 該方法會將字符分拆為三個部分
# 從字符串高位索引開始规辱,在一次遇到sep時對字符串進行分拆谆棺,會將字符串分拆為3個字符串:
# sep之前的字符構成第一個字符串,sep構成第二個字符串按摘,sep之后的字符構成第三個字符串
>>> 'abcdabcd'.rpartition('cd')
('abcdab', 'cd', '')
>>> 'abcdabcd'.rpartition('ab')
('abcd', 'ab', 'cd')
>>> 'abcd'.rpartition('ab')
('', 'ab', 'cd')
# 如果字符串中沒有sep包券,也會返回三個元組:
# 前兩個字符串均為空纫谅,原字符串構成第三個字符串,
>>> 'abcdabcd'.rpartition('ef')
('', '', 'abcdabcd')
split&rsplit
?? str.split(sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1
elements). If maxsplit is not specified or -1
, then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',')
returns ['1', '', '2']
). The separgument may consist of multiple characters (for example, '1<>2<>3'.split('<>')
returns ['1', '2', '3']
). Splitting an empty string with a specified separator returns ['']
.
# 該方法會以sep作為分隔符溅固,對字符串進行拆解付秕,并返回拆解后的列表
# 拆解操作始于字符的左側(cè)
>>> '1,2,3'.split(',')
['1', '2', '3']
# maxsplit用于指定分解次數(shù);默認值是-1侍郭,表示進行最大限度的拆解
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']
>>> ''.split('-')
['']
>>> 'bcd'.split('a')
['bcd']
# 連續(xù)的分隔符和尾部的分隔符询吴,均會產(chǎn)生空字符串
>>> '1,2,,,3,'.split(',')
['1', '2', '', '', '3', '']
# sep可以包含多個字符
>>> '1<>2<>3'.split('<>')
['1', '2', '3']
If sep is not specified or is None
, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None
separator returns []
.
# 如果sep的值為None,則會將連續(xù)的空白符視為分隔符
>>> '1 2 3'.split()
['1', '2', '3']
>>> '1\t2\n3'.split()
['1', '2', '3']
>>> '1,2,3'.split()
['1,2,3']
>>> '1 2 3'.split(maxsplit=1)
['1', '2 3']
# 字符串的頭部和尾部的空白符亮元,不會產(chǎn)生空字符串
>>> ' 1 2 3 '.split()
['1', '2', '3']
# 拆解僅包含空白符的字符串會返回一個空列表
>>> ' '.split()
[]
>>> ''.split()
[]
?? str.rsplit(sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None
, any whitespace string is a separator. Except for splitting from the right, rsplit()
behaves like split()
which is described in detail below.
# 該方法會以sep作為分隔符猛计,對字符串進行拆解,并返回拆解后的列表
# 拆解操作始于字符的右側(cè)爆捞,其余行為和split()一致
>>> '1,2,3'.rsplit(',', maxsplit=1)
['1,2', '3']
>>> ',1,2,,3,'.rsplit(',')
['', '1', '2', '', '3', '']
>>> '1 2 3'.rsplit(maxsplit=1)
['1 2', '3']
splitlines
?? str.splitlines([keepends])
Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.
該方法會將行邊界符作為分拆點奉瘤,將字符串拆解為由多字符串組成的列表。當 keepends 為 True·
時煮甥,則會在結(jié)果中保留行邊界符盗温。
以下是作為分拆依據(jù)的行邊界符(line boundaries)。注意成肘,行邊界符是通用換行符('\n','\r\n','\r')的超集(universal newlines)
Representation | Description |
---|---|
\n |
Line Feed |
\r |
Carriage Return |
\r\n |
Carriage Return + Line Feed |
\v or \x0b
|
Line Tabulation |
\f or \x0c
|
Form Feed |
\x1c |
File Separator |
\x1d |
Group Separator |
\x1e |
Record Separator |
\x85 |
Next Line (C1 Control Code) |
\u2028 |
Line Separator |
\u2029 |
Paragraph Separator |
Changed in version 3.2: \v
and \f
added to list of line boundaries.
# \r\n 被視作一個整體
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']
Unlike split()
when a delimiter string sep is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line:
# 在遇到空字符串時卖局,splitlines會返回一個空列表
>>> "".splitlines()
[]
>>> "One line\n".splitlines()
['One line']
對比 split('\n')
:
# 在給定sep時,split會在遇到空字符串時返回一個包含空字符串的列表
>>> ''.split('\n')
['']
>>> 'Two lines\n'.split('\n')
['Two lines', '']