Behaviour of str.split

John Machin sjmachin at lexicon.net
Mon Apr 18 17:49:58 EDT 2005


On Mon, 18 Apr 2005 16:16:00 +0100, Will McGugan
<news at NOwillmcguganSPAM.com> wrote:

>Hi,
>
>I'm curious about the behaviour of the str.split() when applied to empty 
>strings.
>
>"".split() returns an empty list, however..
>
>"".split("*") returns a list containing one empty string.
>
>I would have expected the second example to have also returned an empty 
>list. What am I missing?
>

You are missing a perusal of the documentation. Had you done so, you
would have noticed that the actual behaviour that you mentioned is
completely the reverse of what is in the documentation!

"""
Splitting an empty string with a specified separator returns an empty
list. 
If sep is not specified or is None, a different splitting algorithm is
applied. <snip> Splitting an empty string or a string consisting of
just whitespace will return "['']". 
"""

As you stumbled on this first, you may have the honour of submitting a
patch to the docs and getting your name on the roll of contributors.
Get in quickly, before X** L** does :-)

Cheers,

John



More information about the Python-list mailing list