Inconsistent behavior of split on empty string

Bengt Richter bokr at oz.net
Tue Feb 17 17:45:06 EST 2004


On 13 Feb 2004 00:46:00 -0800, bk_kl at gmx.de (bk_kl at gmx.de) wrote:

>Hi,
>
>I think the following behavior of the build in function 'split' is inconsistent.
>What do you think?
>
>>>> "".split()
>[]
>>>> "".split(";")
>['']
>
>I'm using python 2.3.3 on Windows 2000.
>(Perl's split only returns all items up to the last non-empty item,
> e.g. <perl>split /;/, "; ;;;"; gets you ['', ' ']. I find this confusing, too).

.split() is not an information-preserving split. It is a convenient special case
whose function should not be confused with that of .split(something).

The latter would be buggy if you could not successfully do this:

 >>> anystring = ''
 >>> somestring = ';'
 >>> assert anystring == somestring.join(anystring.split(somestring))
 >>> anystring == somestring.join(anystring.split(somestring))
 True

Unfortunately (other than making a likely bug announce itself),

 >>> somestring = ''
 >>> anystring == somestring.join(anystring.split(somestring))
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 ValueError: empty separator

but otherwise I think it works.

Regards,
Bengt Richter



More information about the Python-list mailing list