str.split() with empty separator

David C Ullrich dullrich at sprynet.com
Thu Sep 17 14:42:56 EDT 2009


On Tue, 15 Sep 2009 14:31:26 +0200, Ulrich Eckhardt wrote:

> Hi!
> 
> "'abc'.split('')" gives me a "ValueError: empty separator". However,
> "''.join(['a', 'b', 'c'])" gives me "'abc'".
> 
> Why this asymmetry?

The docs say 

"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']). "

Now suppose sep = ''. That means split() should return an infinitely
long list of empty strings! Because if sep = '' then the
string 'hello' starts with an empty string followed by sep
followed by an empty string followed by sep followed by an
empty string followed by sep... that's all before we get to
the 'h'.


> I was under the impression that the two would be
> complementary.
> 
> Uli




More information about the Python-list mailing list