Reputation: 558
I am using the str.split()
method and I can pin down what causes the difference in result, but I don't understand why it is.
>>> dummy_line = "Line1 \nLine2 \nLine3"
>>> print(dummy_line.split())
['Line1', 'Line2', 'Line3']
>>> print(str.split(" "))
['Line1', '\nLine2', '\nLine3']
Why does defining the split delimeter as " "
in the second instance result in the returned lines including the new line escape character \n
?
Upvotes: 0
Views: 52
Reputation: 191738
str.split(" ")
separates on a single space. Newlines therefore are kept.
str.split()
breaks all whitespace. (\s
, \r
, \n
, \t
)
Maybe your misunderstanding is Python's support for default function arguments?
Hint: Try dummy_line = "Line 1\nLine 2"
Upvotes: 3
Reputation: 876
Upvotes: 0