converting an array of chars to a string

Derek Thomson derek at wedgetail.com
Fri Jun 21 07:02:09 EDT 2002


Gerhard Häring wrote:
> 
> 
> Unless Python 1.5.2 compatibilty is a requirement, just use string
> methods:
> 
>     lst = ['a', 'b', 'c', 'd']
>     s = ''.join(lst)

And they say you can't write obfuscated code in Python :)

I would have expected "join" to be a method on the sequence, if 
anything. As in "s = lst.join('')". In fact, I looked for it, as I 
*knew* I'd seen it used before as a method, I just didn't expect it to 
be in the string class.

I think I'll just stick with string.join for the time being. As it turns 
out, there's more than one way to do it, even in Python ;)

< It's also not a good idea to replace the builtin list function, thus
 > I'm using 'lst' here.

You're right, of course.

Regards,
Derek.






More information about the Python-list mailing list