Join equivalent for tuples of non-strings?

Peter Hansen peter at engcorp.com
Thu Sep 18 13:59:48 EDT 2003


"Peter L. Buschman" wrote:
> 
> I'm trying to think in Python, but am stumped here...
> 
> What is the equivalent for the following if you are dealing with a tuple
> of non-strings?
> 
> >>> import string
> >>> foo = ( '1', '2', '3' )
> >>> string.join( foo, '.' )
> '1.2.3'
> 
> With a tuple of integers, this fails with a traceback as below:

Try this instead, using old and new style Python:

string.join(map(str, foo), '.')

or 

'.'.join([str(x) for x in foo])

-Peter




More information about the Python-list mailing list