I am learning Python. In particular, i read about the python string's split method and came to know that the default separator for split
is a whitespace. So i understand how the following works:
text = 'Hello World'
print(text.split()) #this should print ['Hello', 'World'] which it does
The output of the above program in Python3.6 is ['Hello', 'World']
as expected because in the above string variable text
we have whitespace.
But then i tried out the following example:
text = 'Hello\nWorld'
print(text.split()) #this should print ['Hello\nWorld'] but it doesn't
The actual output of the above is:
['Hello', 'World'] #this shouldn't happen because there is no whitespace in text
While the expected output of the above is:
['Hello\nWorld'] #this should happen because there is no whitespace in text
As you can see, since there is not whitespace between 'Hello'
and 'World'
the output should be ['Hello\nWorld']
because \n
is not a whitespace and a whitespace is the default separator for split
method.
What is happening here.
CodePudding user response:
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.
Tabs (\t
), newlines (\n
), spaces, etc. They all count as whitespace characters as technically they all serve the same purpose. To space things out.