tuple to string?

F

Francois De Serres

hiho,

what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
the string 'spam'?

TIA,
Francois
 
G

Guest

Francois De Serres said:
hiho,

what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D)
to the string 'spam'?

..>>> t = (0x73, 0x70, 0x61, 0x6D)
..>>> ''.join('%c' % c for c in t)
'spam'
 
R

Reinhold Birkenfeld

Berthold said:
.>>> t = (0x73, 0x70, 0x61, 0x6D)
.>>> ''.join('%c' % c for c in t)
'spam'

Or:

t = (0x73, 0x70, 0x61, 0x6D)
('%c' * len(t)) % t

Reinhold
 
R

Reinhold Birkenfeld

John said:
You don't need the sissy parentheses; '%c' * len(t) % t works just fine :)

Ah, ok. Didn't want to lookup the precedence rules...

Reinhold
 
J

John Machin

Reinhold said:
Ah, ok. Didn't want to lookup the precedence rules...


Look up the precedence rules? Are you aware of any language where * /
and % _don't_ have the same precedence??
 
R

Robert Kern

John said:
Reinhold Birkenfeld wrote:

Look up the precedence rules? Are you aware of any language where * /
and % _don't_ have the same precedence??

Given that % is somewhat more esoteric, I certainly have never committed
to memory its position in a precedence hierarchy of *any* language.

--
Robert Kern
(e-mail address removed)

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

Steven D'Aprano

Look up the precedence rules? Are you aware of any language where * /
and % _don't_ have the same precedence??

Do languages like Pascal that don't have string formatting expressions, or
use the % operator, count?

How about languages like Forth that don't have precedence rules at all,
unless "first come, first served" is a precedence rule?

I'm not being academic here. I have used both these languages extensively,
admittedly many years ago.
 
J

John Machin

Steven said:
Do languages like Pascal that don't have string formatting expressions, or
use the % operator, count?

A thousand pardons; I should have said "Are you aware of any language
which has % (as primarily a numeric remainder/modulo operator) but * /
and % _don't_ have the same precedence??"

OK, given a language which does have * and / used among other things for
numerical multiply and divide, (a) are you aware of any such language
which does does not have * and / at the same precedence level (b)
supposing one wanted to introduce % as a numerical
remainder/modulo/whatever operator (plus other meaning(s) for
non-numeric types), would you care to argue that it should not have the
same precedence level (as * and /)?

Pascal was/is a prime example of bad precedence choice:
a > b or c > d
means
a > (b or c) > d
in Pascal (not very useful)
and
(a > b) or (c > d)
in many other languages.

How about languages like Forth that don't have precedence rules at all,
unless "first come, first served" is a precedence rule?

No precedence rules -> no relevance to the topic
 
R

Robert Kern

John said:
No precedence rules -> no relevance to the topic

Precedence rules of other languages -> no relevance to the topic

--
Robert Kern
(e-mail address removed)

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

Steven D'Aprano

A thousand pardons; I should have said "Are you aware of any language
which has % (as primarily a numeric remainder/modulo operator) but * /
and % _don't_ have the same precedence??"

[slaps head]

Ah, I had completely forgotten that Pascal has a MOD operator that is
equivalent to % and has the same precedence as * / and DIV. So scratch
Pascal off the list.

But APL uses right-to-left precedence for all operators, and Forth uses
left-to-right. There may be others.

OK, given a language which does have * and / used among other things for
numerical multiply and divide, (a) are you aware of any such language
which does does not have * and / at the same precedence level (b)
supposing one wanted to introduce % as a numerical
remainder/modulo/whatever operator (plus other meaning(s) for
non-numeric types), would you care to argue that it should not have the
same precedence level (as * and /)?

Yes I would.

Since the remainder (or modulo) operator is not distributive, the only
unambiguous usage is to use parentheses, or to decide on precedence rules.
The usual mathematical convention is that modulus has lower precedence
than addition, eg in "clock arithmetic" we expect that three hours after
ten is one: 10+3 modulo 12 is 1, not 13.
 
S

Steven D'Aprano

Precedence rules of other languages -> no relevance to the topic


I thought the topic was -- or at least had wandered in the direction of --
whether or not it was unthinkable for the precedence of % to be
anything but that of multiplication and division. Surely the precedence
rules of other languages have some relevance to that question.

Still, the subject is rapidly losing whatever interest it may have had.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,310
Members
47,977
Latest member
MillaDowdy

Latest Threads

Top