[Tutor] Format strings for variables - how?

David Porter jcm@bigskytel.com
Thu, 8 Feb 2001 00:28:39 -0700


* Viktor Lakics <lakicsv@usa.net>:
> Hi dear Tutors,
> 
> Sorry for the basic question, I am just a starter in Python...

This is what tutor is for!

> a=1
> b=3
> print "%0.2f" %(a/b)
> 
> The above code gives me 0.00 as a result, while I wanted to get
> 0.33...

The reason is that the numbers you are dividing are not floats. Because of
that, you are doing integer division.

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

gives 0.33. You must add a decimal point to one (or more) of the numbers.


David