string formatter for tuple

Steve Holden steve at holdenweb.com
Thu Nov 2 10:39:09 EST 2006


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




More information about the Python-list mailing list