Re: tuple to string?

Discussion in 'Python' started by Francois De Serres, Jul 24, 2005.

  1. 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.

    Francois
     
    Francois De Serres, Jul 24, 2005
    #1
    1. Advertising

  2. Francois De Serres

    John Machin Guest

    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
     
    John Machin, Jul 24, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    812
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    962
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    616
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    831
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    971
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page