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
In Python, you have to specify the type and not rely on inference October 10, 2025 Python
Native connection pooling in Django 5 with PostgreSQL June 25, 2025 Python
Combining Django signals with in-memory LRU cache August 9, 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