473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

tuple to string?

hiho,

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

TIA,
Francois
Jul 22 '05 #1
15 1784
''.join((chr(e) for e in (0x73, 0x70, 0x61, 0x6D)))

Jul 22 '05 #2
Francois De Serres wrote:
hiho,

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

mytuple = (0x73, 0x70, 0x61, 0x6D)
''.join(chr(v) for v in mytuple)

'spam'

Jul 22 '05 #3
Francois De Serres wrote:
hiho,

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


one way is to use a list expression:
''.join([chr(c) for c in (0x73, 0x70, 0x61, 0x6D)]) 'spam'

another is to use map:
''.join(map(chr, (0x73, 0x70, 0x61, 0x6D)))

'spam'

HTH,
deelan.

--
deelan, #1 fan of adriana lima!
<http://www.deelan.com/>


Jul 22 '05 #4
Francois De Serres <fd*******@gmx.net> writes:
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'

--
"Es gelten die Regeln der christlichen Seefahrt: Rot und Grün markiert
das sichere Fahrwasser, Schwarz und Gelb markieren Untiefen und
Wracks."
Christa Sager, Bundestagsfraktionsvorsitzende Bündnis 90/Grüne
Jul 22 '05 #5
Berthold Höllmann wrote:
Francois De Serres <fd*******@gmx.net> writes:
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'


Or:

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

Reinhold
Jul 22 '05 #6
Reinhold Birkenfeld wrote:
Berthold Höllmann wrote:
Francois De Serres <fd*******@gmx.net> writes:

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'

Or:

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


You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)
Jul 22 '05 #7
John Machin wrote:
Reinhold Birkenfeld wrote:
Berthold Höllmann wrote:
Francois De Serres <fd*******@gmx.net> writes:
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'

Or:

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


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
Jul 23 '05 #8
Reinhold Birkenfeld wrote:
John Machin wrote:
Reinhold Birkenfeld wrote:
Berthold Höllmann wrote:
Francois De Serres <fd*******@gmx.net> writes:

>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'
Or:

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


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

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??
Jul 23 '05 #9
John Machin wrote:
Reinhold Birkenfeld wrote:

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??


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

--
Robert Kern
rk***@ucsd.edu

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

Jul 23 '05 #10
On Sat, 23 Jul 2005 23:31:04 +1000, John Machin wrote:
You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)

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??


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

Jul 24 '05 #11
Steven D'Aprano wrote:
On Sat, 23 Jul 2005 23:31:04 +1000, John Machin wrote:

You don't need the sissy parentheses; '%c' * len(t) % t works just fine :-)
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??

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
Jul 24 '05 #12
On Sun, 24 Jul 2005 21:55:19 +1000, John Machin wrote:
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?


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

Jul 24 '05 #13
John Machin wrote:
No precedence rules -> no relevance to the topic


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

--
Robert Kern
rk***@ucsd.edu

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

Jul 24 '05 #14
On Sun, 24 Jul 2005 10:39:44 -0700, Robert Kern wrote:
John Machin wrote:
No precedence rules -> no relevance to the topic


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

Jul 24 '05 #15
Steven D'Aprano wrote:
Still, the subject is rapidly losing whatever interest it may have had.


It had none. Kill it. "Kill the witch!"
Jul 24 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lukas Kasprowicz | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Folks, My Proglem is, I get after a query on a mysql database with module MySQLdb a tuple but I need this output from database as a string....
4
by: Alastair G. Hogge | last post by:
Hello *, I'm using Python and the cgi module to retrive data from a HTML form. I'm then trying to get that information into a string. But efforts fail when I run type(foo) on my string. type()...
50
by: Will McGugan | last post by:
Hi, Why is that a tuple doesnt have the methods 'count' and 'index'? It seems they could be present on a immutable object. I realise its easy enough to convert the tuple to a list and do this,...
15
by: Steve M | last post by:
Hello, I'm trying to figure out the index position of a tuple member. I know the member name, but I need to know the members index position. I know that if I use the statement print tuple that...
16
by: flyaflya | last post by:
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3)
3
by: Carl J. Van Arsdall | last post by:
From my interpreter prompt: >>> tuple = ("blah") >>> len(tuple) 4 >>> tuple2 = ("blah",) >>> len (tuple2) 1 So why is a tuple containing the string "blah" without the comma of
3
by: localpricemaps | last post by:
i am having a problem writing a tuple to a text file. my code is below. what i end up getting is a text file that looks like this burger, 7up burger, 7up burger, 7up and this is instead...
6
by: ronrsr | last post by:
here is my result. How do I determine the number of tuples in this array, returned from a mysql database? How do I determine the number of characters or entry in each tuple? thanks very much...
77
by: Nick Maclaren | last post by:
Why doesn't the tuple type have an index method? It seems such a bizarre restriction that there must be some reason for it. Yes, I know it's a fairly rare requirement. Regards, Nick...
3
by: Daniel | last post by:
I have a list of strings, which I need to convert into tuples. If the string is not in python tuple format (i.e. "('one', 'two')", "("one", 'two')", etc.), then I can just make it a 1-tuple (i.e....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.