join and split with empty delimiter

Chris Angelico rosuav at gmail.com
Wed Jul 17 17:24:36 EDT 2019


On Thu, Jul 18, 2019 at 7:06 AM Irv Kalb <Irv at furrypants.com> wrote:
> If I join the list with the empty string as the delimiter:
>
> >>> myList = ['a', 'b', 'c', 'd']
> >>> myString = ''.join(myList)
> >>> print(myString)
> abcd
>
> That works great.  But attempting to split using the empty string generates an error:
>
> >>> myString.split('')
> Traceback (most recent call last):
>   File "<pyshell#9>", line 1, in <module>
>     myString.split('')
> ValueError: empty separator
>
> But my question is:  Is there any good reason why the split function should give an "empty separator" error?  I think the meaning of trying to split a string into a list using the empty string as a delimiter is unambiguous - it should just create a list of single characters strings like the list function does here.
>

Agreed. There are a number of other languages where splitting on an
empty delimiter simply fractures the string into characters (I checked
Pike, JavaScript, Tcl, and Ruby), and it's a practical and useful
feature. +1.

ChrisA



More information about the Python-list mailing list