user5319825
user5319825

Reputation: 543

Why split function ignores '\n' even if I don't specify the delimiter?

According to python documentation.

str.split([sep[, maxsplit]])

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 [].
But,

str = 'L1 \nL2 \nL3'
print str.split()

returns ['L1','L2','L3']

Upvotes: 1

Views: 70

Answers (1)

Adam
Adam

Reputation: 17339

Whitespace consists of the characters ' ', '\t', '\r', and '\n'. Therefore, " \n" is a single run of consecutive whitespace.

Upvotes: 9

Related Questions