I want to pad a number i
with a padding based on the value of the variable width
>>> width = 5
>>> i = 1
>>> print(f'{i:width}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier
>>>
>>> print('{:width}'.format(i))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier
Desired output:
1
You almost had it:
>>> width = 5
>>> i = 1
>>> print(f'{i:{width}}')
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With