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:,}"
'1,234,567'

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      '

Comments

Sarma Akondi

NIce 👌

Your email will never ever be published.

Previous:
Leibniz formula for π in Python, JavaScript, and Ruby March 14, 2024 Python, JavaScript
Next:
Search GitHub issues by title, only May 31, 2024 GitHub
Related by category:
A Python dict that can report which keys you did not use June 12, 2025 Python
Autocomplete using PostgreSQL instead of Elasticsearch December 18, 2025 Python
Comparison of speed between gpt-5, gpt-5-mini, and gpt-5-nano December 15, 2025 Python
Using AI to rewrite blog post comments November 12, 2025 Python
Related by keyword:
How to pad/fill a string by a variable in Python using f-strings January 24, 2020 Python
Credit Card formatter in Javascript November 19, 2013 JavaScript
Format thousands in Python February 1, 2019 Python
How to string pad a string in Python with a variable October 19, 2021 Python