4 回答

TA貢獻1802條經驗 獲得超4個贊
根據文檔(針對 Python 3.8,并強調):
如果sep未指定或為None,則應用不同的拆分算法:連續空格的運行被視為單個分隔符,如果字符串具有前導或尾隨空格,則結果將在開頭或結尾不包含空字符串。
所以,不,它們不是一回事。例如(注意在開始和結束處有一個和一個之間有兩個空格):AB
>>> s = " A B "
>>> s.split()
['A', 'B']
>>> s.split(" ")
['', 'A', '', 'B', '']
此外,連續的空白意味著任何空白字符,而不僅僅是空格:
>>> s = " A\t \t\n\rB "
>>> s.split()
['A', 'B']
>>> s.split(" ")
['', 'A\t', '', '\t\n\rB', '']

TA貢獻1816條經驗 獲得超6個贊
>>> print ''.split.__doc__
S.split([sep [,maxsplit]]) -> list of strings
Return a list of the words in the string S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are removed
from the result.

TA貢獻1862條經驗 獲得超7個贊
此處的文檔str.split(sep=None, maxsplit=-1)。筆記:
如果 sep 未指定或為 None,則應用不同的拆分算法:連續空格的運行被視為單個分隔符,如果字符串具有前導或尾隨空格,則結果將在開頭或結尾不包含空字符串。因此,用 None 分隔符拆分空字符串或僅由空格組成的字符串會返回 []。
>>> a = " hello world "
>>> a.split(" ")
['', 'hello', 'world', '']
>>> a.split()
['hello', 'world']
>>> b = "hello world"
>>> b.split(" ")
['hello', '', '', '', '', '', '', '', '', '', '', 'world']
>>> b.split()
['hello', 'world']
>>> c = " "
>>> c.split(" ")
['', '', '', '', '', '', '', '']
>>> c.split()
[]
添加回答
舉報