Tuesday 15 March 2011

python - Splitting empty string with space as a separator returns [''] -



python - Splitting empty string with space as a separator returns [''] -

this question has reply here:

when splitting empty string in python, why split() homecoming empty list while split('\n') returns ['']? 6 answers

is right behaviour?

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

shouldn't empty array? mean, there no spaces in empty string there nil split, should [], no?

when utilize none separator, empty array:

>>> ''.split() []

i think doc has explained behavior

if sep given, consecutive delimiters not grouped , deemed delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). splitting empty string specified separator returns [''].

if sep not specified or none, different splitting algorithm applied: runs of consecutive whitespace regarded single separator, , result contain no empty strings @ start or end if string has leading or trailing whitespace. consequently, splitting empty string or string consisting of whitespace none separator returns [].

python

No comments:

Post a Comment