This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: "string".split behaviour for empty strings
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: meyarivan, nbastin, rhettinger, terry.reedy
Priority: normal Keywords:

Created on 2003-09-24 08:20 by meyarivan, last changed 2022-04-10 16:11 by admin. This issue is now closed.

Messages (6)
msg18312 - (view) Author: T.Meyarivan (meyarivan) Date: 2003-09-24 08:20
"".split() -> []

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

It is not clear from the documentation as to what is
the defined behaviour in the above case. To say the
least, it is 
completely counter-intuitive..(splitting an empty
string should
return the list [''] in both cases).

From stringobject.c, the behaviour of split_whitespace
(i.e split called without sep) and string_split (split
called with sep) is different for empty
strings..perhaps the function split_whitespace needs to
be patched.
msg18313 - (view) Author: T.Meyarivan (meyarivan) Date: 2003-09-24 08:51
Logged In: YES 
user_id=30470

"".split(None) -> [] 

which is expected because in the case when no sep is
specified and when sep == None, split_whitespace is called..
msg18314 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-09-26 04:35
Logged In: YES 
user_id=80475

And so it is.  The whitespace version (no args) and the
specified separator version work differently -- the second
version can never return an empty list.

This code has been around for a good while and it is likely
that much code relies on the behavior, so I recommend
classifying it as a beauty spot and closing the bug.

"One man's bug is another man's feature"
msg18315 - (view) Author: T.Meyarivan (meyarivan) Date: 2003-09-26 11:04
Logged In: YES 
user_id=30470


Thats so counter intuitive.. :-(

At least the documentation should specify the fact that the
behaviour of "string".split for empty strings is so and so....

The reason this probably never came up ( or mostly ignored
by majority ) is that people probably check if the string is
empty before proceeding to process it.. 

I am not closing the bug before the specific behaviour of
"string".split for empty strings gets to the docs..
msg18316 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2003-09-28 23:50
Logged In: YES 
user_id=593130

Suggestion: find section of Lib Ref discussing str.split, quote 
existing def, and add succinct sentence that you think is 
needed.  Then change category to 'documentation' so 
appropriate person(s) will see proposal.
msg18317 - (view) Author: Nick Bastin (nbastin) * (Python committer) Date: 2004-03-21 17:11
Logged In: YES 
user_id=430343

Fixed in doc/lib/libstring.tex version 1.57
History
Date User Action Args
2022-04-10 16:11:19adminsetgithub: 39287
2003-09-24 08:20:19meyarivancreate