osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

join and split with empty delimiter


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