0

I was wondering if there is a way in Python > 3.6, using f-strings, to achieve the following: I have a number 2451545.00000 which I would like to print as %.5f but at the same time I'd like to have it centre justified e.g. using f-string {' '}^{20}. Is there a way to achieve that in a single command e.g., {2451545.00000:.5{' '}^{20}} or something like that?

Thank you in advance!

Abedin
  • 23
  • 5

2 Answers2

2

You can use ^ in the f-string:

num = 2451545.00000
print(repr(f"{num:^20.5f}")) # '   2451545.00000    '

You can read the doc for relevant information. The ^ is listed as an "align" option.

j1-lee
  • 13,764
  • 3
  • 14
  • 26
2

I think what you're going for can be written as follows:

my_string = "{0:^20.5f}".format(2451545.00000000)
print(my_string)

Hope it helps.

Deck451
  • 61
  • 5