More random python observations from a perl programmer

Andrew Csillag andrew at starmedia.net
Thu Aug 19 22:31:02 CEST 1999


Tom Christiansen wrote:
>
> As for the tuple thing, why can I say
> 
>     print "You have $%0.2f bucks" % (4.5/100)
> 
> How is that a singleton tuple? 
It's not.  For format strings, if the item (here 4.5/100) is a single
thing, the items need not be a tuple. i.e.

   "You have $%0.2f bucks" % 234.234

but if you want more than one item to be formatted, you have to use a
tuple

   "you're name is %s and you have $%0.2f" % ("Tom", 234.234)

> Don't I have to use
> a trailing comma?  It won't let me!  Why is
> 
>     print "You have $%0.2f bucks" % (4.5/100,)
> 
> illegal, 
No, it's perfectly fine.  Didn't try it, I guess.

>>> print "You have $%0.2f bucks" % (4.5/100,)
You have $0.04 bucks
>>> 


but
> 
>     print "You have $%0.2f bucks" % ((4.5/100,) * 2)
This, on the other hand is wrong since ((4.5/100,) * 2) yields (0.045,
0.045) and you only have one formatting bit in the string (only 1
%0.2f).  If you had 
print "The New York Lotto jackpot is now $%0.2f bucks... $%0.2f" %
((4.5/100,) * 2)
That totally works.

> 
> mandatory?  Was this (elt,) thing just an, er, unforeseen design
> misfeature? Yes, I tripped me up again. :-(
Try reading the docs...
-- 
"Programmers are just machines that convert coffee to executable code"
   -Unknown

"Drew Csillag" <drew_csillag at geocities.com>




More information about the Python-list mailing list