3 ms·
In that example str.split() has the same result as str.splitlines(), but it's not in general the same, even without custom delimiter. str.split() splits on run
by roelschroeven 1y ago
In that example str.split() has the same result as str.splitlines(), but it's not in general the same, even without custom delimiter.
str.split() splits on runs of consecutive whitespace, any type of whitespace, including tabs and spaces which splitlines() doesn't do.
>>> 'one two'.split()
['one', 'two']
>>> 'one two'.splitlines()
['one two']
split() without custom delimiter also splits on runs of whitespace, which splitline() also doesn't do (except for \r\n because that combination counts as one line ending):
>>> 'one\n\ntwo'.split()
['one', 'two']
>>> 'one\n\ntwo'.splitlines()
['one', '', 'two']