tuple to string?

Patricia J. Hawkins phawkins at connact.com
Fri Jul 22 20:37:47 EDT 2005


QH> On 7/22/05, Francois De Serres <fdeserres at gmx.net> wrote:
>> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
>> the string 'spam'?

QH> Use ''.join and chr() as others have pointed out.  Here are
QH> just-for-fun versions  ;)

.> t = (0x73, 0x70, 0x61, 0x6D)

QH> (use string formatter):
.> '%c%c%c%c' % t

Or more generally:

>>> t = (0x73, 0x70, 0x61, 0x6D)
>>> '%c'*len(t) % t
'spam'

but that's VERY perlonic python.  Still, it's a technique that can
come in handy when building, say, SQL queries on the fly.

-- 
Patricia J. Hawkins
Hawkins Internet Applications
www.hawkinsia.com



More information about the Python-list mailing list