On Fri, 22 Jul 2005 06:07:28 -0700, Robert Kern wrote: > Francois De Serres wrote: >> hiho, >> >> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to >> the string 'spam'? > > In [1]: t = (0x73, 0x70, 0x61, 0x6D) > > In [2]: ''.join(chr(x) for x in t) > Out[2]: 'spam'
I get a syntax error when I try that. I guess anyone who hasn't started using Python 2.4 will also get the same error. Since t is just a tuple, there isn't a big advantage as far as I can see to build up and dispose of the generator machinery just for grabbing the next item in a tuple. So a list comprehension will work just as well, and in older versions of Python: ''.join([chr(x) for x in (0x73, 0x70, 0x61, 0x6D)]) For an even more version-independent method: L = [] for n in (0x73, 0x70, 0x61, 0x6D): L.append(chr(n)) print ''.join(L) or even: >>> ''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D))) 'spam' -- Steven. -- http://mail.python.org/mailman/listinfo/python-list