4

Could someone explain this behaviour on python 2.7.8:

Python 2.7.8 (default, Nov 12 2014, 02:03:09)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = ''
>>> a.split()
[]
>>> a.split('\n')
['']

split by any white space gives an empty list, but split by new line gives a list with an empty string. WHY?

Thanks

0

4 Answers 4

5

From python str.split docs (https://docs.python.org/2/library/stdtypes.html#str.split):

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

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

Sign up to request clarification or add additional context in comments.

1 Comment

You should also mention that the latter splitting algorithm splits on all white space, not just spaces. Which isn't immediately obvious from the documentation.
4

Based on python wiki :

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

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].

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

For more explanation read this answer too https://stackoverflow.com/a/16645307/2867928

2 Comments

ok, I see. I still find this confusing though... Thanks.
@WeaselFox pleas read the second part its very clear! i'll add more explain in answer!
1

Providing any character will create a list:

>>> a = ''
>>> a.split()
[]
>>> a.split(' ')
['']

Regardless of the character. It behaves this way because it looks for a character, splits the string into a list, and gives list items for the left and right of the character.

>>> a = 'This is a test.'
>>> a.split('a')
['This is ', ' test.']

If there is no matching character, there is nothing on either side of the character.

>>> a = 'Another test string.'
>>> a.split('*')
['Another test string.']

3 Comments

ok... but why? I find this weird..
Because the split method looks for the character, and splits the string at that point. Since it can't find it, it returns an empty string. @WeaselFox
Please check my update; it should provide an explanation and example for the behavior of split. @WeaselFox
-2

When you tell python to split('\n') it is separating different lines. Therefore returning the first line it finds which is... '' . If your input had more than one line for example: a = line1\nline2

it would print out those lines.

Edit: added \n between line1 and line2, an enter = to a \n

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.