Format float with 2 digits before decimal, 6 after decimal

Question:

E.g. if I have 1.234 I’d like to format it as '01.234000.

I can get:

>>> f'{1.234:.6f}'
'1.234000'

but I also need a leading digit – how can I specify that within the f-string?

Asked By: ignoring_gravity

||

Answers:

The formatting specification takes a width before the period, and a zero in front of that to say you want leading zeros instead of leading spaces:

>>> f'{1.234:09.6f}'
'01.234000'
Answered By: joanis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.