1

Can you create a f-string where you use a integer variable for the digits of padding?

>>> s = 8
>>> f"{'test':s}"

With .format() you could solve this doing something like:

>>> "{{:{}}}".format(s).format("test")

I'd use it for printing a list of names, padded by max name length:

s = max(map(len, names))
for n in names:
    print(f"The age of {n:s} is {ages[n]}.")
2
  • 2
    n, s = "test", 8; f'{n:{s}}' -> 'test ' Commented Dec 9, 2020 at 10:14
  • 2
    Also, you don't have to use multiple calls to str.format to do the same with it: "{:{}}".format(n, s) Commented Dec 9, 2020 at 10:25

1 Answer 1

0

Put the variable format specifier in curly brackets:

>>> s = 8
>>> f"{'test':s}"
'test'
>>> f"{'test':{s}}"
'test    '
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.