Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multiple options with the .format() function in Python

I read the section about .format() function in the Python3.3 documentation and did some online research, but unfortunately I only find examples and explanations for "single" options.

Here is an example:

  • printing right aligned

>>> print("this is a {:>30} test ".format(2.345345345345))

this is a                 2.345345345345  test 
  • printing only 2 digits after the decimal point

>>> print("this is a {:.2f} test ".format(2.345345345345))

this is a 2.35  test

But how would I do both at once? I already tried various variation, but unfortunately without success. Anyone knows the correct syntax?


1 Answers

Try

print("this is a {:>30.2f} test ".format(2.345345345345))

When in doubt, try the obvious solutions.

like image 95
Anorov Avatar answered Jun 18 '26 11:06

Anorov