Home > Back-end >  What does {foo:>7f} do in Python f-strings?
What does {foo:>7f} do in Python f-strings?

Time:09-21

In particular, this is over my head:

print(f"foo: {foo:>7f}, bar: {bar:>5d}")

I can imagine that f indicates float and d indicates integer but I don't really understand what the >7f and >5d do.

Note that I understand what

print(f"foo: {foo}, bar: {bar}")

does.

CodePudding user response:

It means that the resulting string from {foo:>7f} should at least be of width 7, meaning that if it were 4 characters/digits long, then spaces would be appended to its left.

>>> foo = 1234
>>> bar = 100
>>> f"foo: {foo:>7d}, bar: {bar:>5d}"
'foo:    1234, bar:   100'

Note the spaces are before each number.

>>> f"foo: {foo:>4d}, bar: {bar:>5d}"
'foo: 1234, bar:   100'

Notice how the first number isn't affected because it has width of 4.

  • Related