string.join() syntax quirky?

Erik Max Francis max at alcyone.com
Thu Nov 22 20:00:17 CET 2001


Erik Johnson wrote:

>     I have a list of single character strings. A couple of people were
> kind enough to clue me in to the best way to turn this back to a
> string:
> 
> s = "".join(l)
> 
>     This works fine, but it seems syntactically backwards to me:
> 
> # s = l.join("")
> 
> makes much more sense to me. The thingy I want to do something to is
> my
> list. I am just trying to learn Python, so no doubt there will be some
> Python things I will just have to get used to, but is there a logical
> reason why it was implemented as a string method rather than a list
> method? Does it seem backwards to you or does it make intuitive sense
> to
> you? Comments?

It may seem a little strange at first, but it makes sense in terms of
what's going on here.  What you want to do is string processing, so it's
much more natural that the method belongs in a string instead of in a
list.  Since the only thing that's a string here is the delimiter,
that's why you have the "".join(l) notation.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Laws are silent in time of war.
\__/ Cicero
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.



More information about the Python-list mailing list