Pad python floats Pad python floats python python

Pad python floats


'%03.1f' works (1 could be any number, or empty string):

>>> "%06.2f"%3.3'003.30'>>> "%04.f"%3.2'0003'

Note that the field width includes the decimal and fractional digits.


Alternatively, if you want to use .format:

              {:6.1f}                ↑ ↑                 | |# digits to pad | | # of decimal places to display

Copy paste: {:6.1f}

The 6 above includes digits to the left of the decimal, the decimal marker, and the digits to the right of the decimal.

Examples of usage:

'{:6.2f}'.format(4.3)Out[1]: '  4.30'f'{4.3:06.2f}'Out[2]: '004.30''{:06.2f}'.format(4.3)Out[3]: '004.30'


You could use zfill as well,.

str(3.3).zfill(5)'003.3'