str.split() with empty separator

Dave Angel davea at ieee.org
Tue Sep 15 09:35:58 EDT 2009


Ulrich Eckhardt wrote:
> Hi!
>
> "'abc'.split('')" gives me a "ValueError: empty separator".
> However, "''.join(['a', 'b', 'c'])" gives me "'abc'".
>
> Why this asymmetry? I was under the impression that the two would be
> complementary.
>
> Uli
>
>   
I think the problem is that join() is lossy;  if you try  "".join(['a', 
'bcd', 'e'])   then there's no way to reconstruct the original list with 
split().  Now that can be true even with actual separators,  but perhaps 
this was the reasoning.

Anyway, if you want to turn a string into a list of single-character 
strings, then use
   list("abcde")

DaveA




More information about the Python-list mailing list