473,386 Members | 1,832 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,386 software developers and data experts.

string formatter for tuple

I have the following in my code

a = (1,2,3)
print "a = %s" %a

But when I run this, I get:

TypeError: not all arguments converted during string formatting

Now I realize why this happens, a is actually 3 elements when the print
statement is only expecting to print one value. I tried

print "a = %s" %(a)

but I got the same error.

How can I print a tuple with a single string format?
Thanks,
Jeremy

Nov 2 '06 #1
6 2437
a = (1,2,3)
print "a = %s" %a

But when I run this, I get:

TypeError: not all arguments converted during string formatting

Now I realize why this happens, a is actually 3 elements when the print
statement is only expecting to print one value. I tried

print "a = %s" %(a)

but I got the same error.

How can I print a tuple with a single string format?

You can try

print "a = %s" % str(a)

or

print "a = %s" % repr(a)

which should both do the trick.

-tkc

Nov 2 '06 #2
jeremito wrote:
I have the following in my code

a = (1,2,3)
print "a = %s" %a

But when I run this, I get:

TypeError: not all arguments converted during string formatting

Now I realize why this happens, a is actually 3 elements when the print
statement is only expecting to print one value. I tried

print "a = %s" %(a)

but I got the same error.

How can I print a tuple with a single string format?
Thanks,
Jeremy
Try

print "a = %s" % (a, )

The exception to the "a single object can be formatted as a single
argument rather than a tuple" rule is when that single object is itself
a tuple!

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 2 '06 #3
Tim Chase wrote:
print "a = %s" % repr(a)
or
print "a = %r" % (a,)
Nov 2 '06 #4
In <11*********************@b28g2000cwb.googlegroups. com>, jeremito wrote:
I have the following in my code

a = (1,2,3)
print "a = %s" %a

But when I run this, I get:

TypeError: not all arguments converted during string formatting

Now I realize why this happens, a is actually 3 elements when the print
statement is only expecting to print one value. I tried

print "a = %s" %(a)

but I got the same error.

How can I print a tuple with a single string format?
Put the tuple in a tuple:

print 'a = %s" % (a,)

And remember: not the parenthesis create a tuple but the comma!

Ciao,
Marc 'BlackJack' Rintsch
Nov 2 '06 #5
>>>>"Tim" == Tim Chase <py*********@tim.thechases.comwrites:
>How can I print a tuple with a single string format?
Tim print "a = %s" % str(a)
Timor
Tim print "a = %s" % repr(a)

Or wrap the tuple in a tuple:

print "a = %s" % (a,)

Skip
Nov 2 '06 #6

sk**@pobox.com wrote:
>>>"Tim" == Tim Chase <py*********@tim.thechases.comwrites:
>How can I print a tuple with a single string format?

Tim print "a = %s" % str(a)
Timor
Tim print "a = %s" % repr(a)

Or wrap the tuple in a tuple:

print "a = %s" % (a,)

Skip
Thank you all very much for your help.
Jeremy

Nov 2 '06 #7

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

Similar topics

3
by: Chrigel | last post by:
Hi all, We have problems deseralizing objects previously serialized as XML. This did work fine with .NET 1.1 but since we have installed SP1, deserializing fails (but serializing works). The...
6
by: Marco Herrn | last post by:
Hi, I need to serialize an object into a string representation to store it into a database. So the SOAPFormatter seems to be the right formatter for this purpose. Now I have the problem that...
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
7
by: Matt | last post by:
I am attempting to reformat a string, inserting newlines before certain phrases. For example, in formatting SQL, I want to start a new line at each JOIN condition. Noting that strings are...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.