3

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

WeaselFox
  • 7,220
  • 8
  • 44
  • 75

4 Answers4

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

ndpu
  • 22,225
  • 6
  • 54
  • 69
  • 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. – will Mar 10 '15 at 18:01
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

Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188
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.']
Zach Gates
  • 4,045
  • 1
  • 27
  • 51
-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

Alan
  • 361
  • 3
  • 22