# Re: tuple to string?

Discussion in 'Python' started by Robert Kern, Jul 22, 2005.

1. ### Robert KernGuest

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'

--
Robert Kern

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Robert Kern, Jul 22, 2005

2. ### Steven D'ApranoGuest

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.

Steven D'Aprano, Jul 23, 2005

3. ### Scott David DanielsGuest

Steven D'Aprano wrote:
> On Fri, 22 Jul 2005 06:07:28 -0700, Robert Kern wrote:
> ... or even:
>
>>>>''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))

>
> 'spam'

This is exactly what is wrong with lambda. It yearns for over-use.
This last should be:

>>>''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))

--Scott David Daniels

Scott David Daniels, Jul 23, 2005
4. ### John MachinGuest

Steven D'Aprano wrote:

>
>
>>>>''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))

>
> 'spam'

Why the verbal diarrhoea? What's wrong with the (already posted)

''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))

???

John Machin, Jul 23, 2005
5. ### Steven D'ApranoGuest

On Sat, 23 Jul 2005 23:26:19 +1000, John Machin wrote:

> Steven D'Aprano wrote:
>
>>
>>
>>>>>''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))

>>
>> 'spam'

>
> Why the verbal diarrhoea?

One line is hardly verbal diarrhoea.

> What's wrong with the (already posted)
>
> ''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))
>
> ???

Nothing.

If I had seen the already posted solution using chr on its own without
lambda, I wouldn't have bothered posting the lambda solution. But I
didn't, so I did.

As another poster has already pointed out, lambda cries out for over-use,
and this was a perfect example of it.

--
Steven.

Steven D'Aprano, Jul 24, 2005
6. ### John MachinGuest

Steven D'Aprano wrote:
> On Sat, 23 Jul 2005 23:26:19 +1000, John Machin wrote:
>
>
>>Steven D'Aprano wrote:
>>
>>
>>>
>>>>>>''.join(map(lambda n: chr(n), (0x73, 0x70, 0x61, 0x6D)))
>>>
>>>'spam'

>>
>>Why the verbal diarrhoea?

>
>
> One line is hardly verbal diarrhoea.
>
>
>>What's wrong with the (already posted)
>>
>>''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))
>>
>>???

>
>
> Nothing.
>
> If I had seen the already posted solution using chr on its own without
> lambda, I wouldn't have bothered posting the lambda solution. But I
> didn't, so I did.
>
> As another poster has already pointed out, lambda cries out for over-use,
> and this was a perfect example of it.

Here are a couple of reductions you can use in future, in the order given:

(1)
lambda <args>: foo(<args>) -> foo # for *any* function foo, not just chr

(2)
lambda <args>: <almost_any_guff>
->
def meaningful_func_name(<args>):
<almost_any_guff>

John Machin, Jul 24, 2005