tuple to string?
John Machin
sjmachin at lexicon.net
Sun Jul 24 08:42:28 EDT 2005
Francois De Serres wrote:
> Francois De Serres wrote:
>
>> hiho,
>>
>> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
>> to the string 'spam'?
>>
>> TIA,
>> Francois
>>
>>
> thanks to all!
>
> I'll pick ('%c' * len(t)) % t, for it's readability and the fact that
> join() is on the deprec'd list.
I presume you mean "deprecated".
AFAIK there is no such thing as a "deprecated list".
Certain constructs cause a deprecation warning to be emitted at run time
-- like passing a float argument where an int is expected.
Other constructs could be loosely described as deprecated because there
is now a better way to do it, but no messages are generated. This is so
for almost all of the functions in the string module. One example of
this is "join": instead of string.join(alist, sep) one now does
sep.join(alist)
Given a non-string sequence of single characters, the
common/standard/well-known idiom for producing a string uses join; it is
''.join(seq)
Backing up to readability, I wouldn't have picked
"('%c' * len(t)) % t" (nor the version with 2 fewer parentheses!) as
particulary readable -- mainly because %c is AFAIK relatively little
used in Python and only someone familar with C etc would understand why
it works, or why it even exists. OTOH something like "''.join(chr(x) for
x in t)" is made up of well-known frequently-used components.
Cheers,
John
More information about the Python-list
mailing list