String formatting with floats... can't get it!

Skip Montanaro skip at
Wed Mar 24 18:56:06 CET 2004

    >>> '%2.2f' % 2.33

    Facundo> Why in this last line I don't get ' 2.33'?

Because the total field width you declared was only 2.  You need a field
width of at least 5 to get a space preceeding the one's digit:

    >>> '%5.2f' % 2.33
    ' 2.33'

The two digits in the format specifier don't add.  The first digit is the
overall width of the field.  The second digit is how much precision to
display to the right of the decimal point.


More information about the Python-list mailing list