Bug or feature? 'abc'.split('') rejects empty separator

John Machin sjmachin at lexicon.net
Mon Feb 11 03:35:13 CET 2002


Erik Max Francis <max at alcyone.com> wrote in message news:<3C6703F4.8F25BE12 at alcyone.com>...
> Neil Schemenauer wrote:
> 
> > Bengt Richter wrote:
> >
> > > Wouldn't it make sense to return list('abc') ?
> > 
> > It would also make sense to return ['a', 'b', 'c'].  Since it's not
> > obvious what you want Python raises an error.
> 
> That's what list('abc') would return.  But in actuality, for it to be
> consistent with the rest of string.split's behavior, 'abc'.split('')
> would have to return ['', 'a', 'b', 'c', ''], not ['a', 'b', 'c'].
> 
> Another beautiful example of why string.split('') should raise an error
> instead of trying to guess what you mean.

How about this for consistency:

>>> " a b c ".split(" ")
['', 'a', 'b', 'c', '']
>>> " a b c ".split()
['a', 'b', 'c']



More information about the Python-list mailing list