[Python-ideas] str.split with empty separator
MRAB
python at mrabarnett.plus.com
Thu Jul 29 18:28:46 CEST 2010
Alexandre Conrad wrote:
> Hello all,
>
> What if str.split could take an empty separator?
>
>>>> 'banana'.split('')
> ['b', 'a', 'n', 'a', 'n', 'a']
>
> I know this can be done with:
>
>>>> list('banana')
> ['b', 'a', 'n', 'a', 'n', 'a']
>
> I think that, semantically speaking, it would make sens to split where
> there are no characters (in between them). Right now you can join from
> an empty string:
>
> ''.join(['b', 'a', 'n', 'a', 'n', 'a'])
>
> So why can't we split from an empty string?
>
> This wouldn't introduce any backwards incompatible changes as
> str.split currently can't have an empty separator:
>
>>>> 'banana'.split('')
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: empty separator
>
> I would love to see my banana actually split. :)
>
Shouldn't it be this:
>>> 'banana'.split('')
['', 'b', 'a', 'n', 'a', 'n', 'a', '']
After all, the separator does exist at the start and end of the string:
>>> 'banana'.startswith('')
True
>>> 'banana'.endswith('')
True
More information about the Python-ideas
mailing list