[Tutor] understanding join

Alan Gauld alan.gauld at btinternet.com
Thu Jul 31 07:49:15 CEST 2008


"Steve Poe" <steve.poe at gmail.com> wrote 

>> In [3]: people = [ 'Tom', 'Dick', 'Harry' ]

> Okay, now let's join people to people and what do we get?

An error, join only works on a single string.

It joins the elements of a sequence of strings into a single
string using the 'owning' string. In some ways, from an 
OOP point of view the method is counterintuitive. It should 
really be a method of a sequejnce taking a string as argument:

[1,2,3].join('/')

makes more sense to me than

'/'.join(['1','2','3'])

But the second is the correct form.
I found the string module function more readable

import string
string.join(['1','2','3'], '/')

Not least because you could omit the second argument 
and get a default space. Making join a member of the 
sequence would have allowed the default behaviour 
to continue. But I assume there were subtle snags 
with that scheme.

Just my personal opinion...

Alan G.




More information about the Tutor mailing list