I have a string like

"{0:.2f}sec/{1}".format(my_variable, other_variable) 

my_variable is a float . I want it to take 5 characters. For a regular variable, I would do this:

 "{0: <5}".format(my_variable) 

I tried:

 "{0:.2f <5} }sec/{1}".format(my_variable, other_variable) "{0:.2f: <5} }sec/{1}".format(my_variable, other_variable) 

but crashes

 ValueError: Invalid conversion specification 

How to make my_variable take up 5 characters? Examples of output:

 "23.45" " 2.34" " 0.30" 
  • "Total" 5 characters? Including point and integer part? Like 0.000 42.00 155.0? - vp_arth
  • one
    yes, including point and integer part. An example of the output that I am trying to get: 12.22 , `0.23 , 1.70`, that is, the missing numbers are replaced with spaces. - nick_gabpe

1 answer 1

Format: {:5.2f}

 >>> '{:5.2f}'.format(3.3) ' 3.30'