tuple to string?

Qiangning Hong hongqn at gmail.com
Fri Jul 22 17:48:19 CEST 2005

Francois De Serres wrote:
> hiho,
> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to 
> the string 'spam'?

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

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

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

(use struct model):
.>>> import struct
.>>> struct.pack('bbbb', *t)

Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
     -- Sybren Stuvel @ c.l.python

Get Firefox! 

More information about the Python-list mailing list