For some reason, I always forget how to do this. Tired of that. Let's blog about it so it sticks.

To format a number with thousand-commas you do:

>>> n = 1234567
>>> f"{n:,}"

To add whitespace to a string you do:

>>> name="peter"
>>> f"{name:<20}"
'peter               '

How to combine these in one expression, you do:

>>> n = 1234567
>>> f"{n:<15,}"
'1,234,567      '


Sarma Akondi

NIce 👌

Your email will never ever be published.

Related posts