[Tutor] Format strings for variables - how?

alan.gauld@bt.com alan.gauld@bt.com
Thu, 8 Feb 2001 17:58:32 -0000


> a=1
> b=3
> print "%0.2f" %(a/b)
0.00
> print "%0.2f" %(0.3333333)
0.33

That's because a and b are integers(aka whole numbers) 
so Python gives a whole number back ie. 0!

Try making either a or b a fraction/floating point/real 
number(choose your term :-) thus:

>>> a = 1.0
>>> b = 3
>>> print "%0.2f" % a/b
0.33

This is explained in more detail in my tutorial on 
the simple equences page.

Alan g.
http://www.crosswinds.net/~agauld/