3

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?

2 Answers 2

2

Try

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

When in doubt, try the obvious solutions.

0
1

Like so:

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