Join strings - very simple Q.

Duncan Booth duncan.booth at invalid.invalid
Sat Mar 24 10:30:06 EDT 2007


"Dustan" <DustanGroups at gmail.com> wrote:
> Perhaps you're doing it wrong, despite having an example right in
> front of you?
> 
> Side by side comparison:
> jl=string.join(l,',')
> jl=','.join(l)
> 
> The sequence is passed as an argument to the join method, and the
> delimiter is the string whose method is being called.
> 

In case you are feeling that the ','.join(l) looks a bit jarring, be aware 
that there are alternative ways to write it. You can call the method on the 
class rather than the instance:

   jl = str.join(',', l)
   jl = unicode.join(u'\u00d7', 'l')

has the advantage of looking almost the same as the string function except 
that the order of the arguments is reversed, the catch is you need to know 
the type of the separator in advance. If you have a lot of calls with the 
same separator then you can also save the method in an appropriately named 
variable:

   commaseparated = ','.join
   ...
   jl = commaseparated(l)




More information about the Python-list mailing list