Format number with space as thousands separator in Python

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Format number with space as thousands separator in Python #

To format a number with a space as the thousands separator:

  1. Use a formatted string literal to format the number with a comma as the thousands separator.
  2. Use the str.replace() method to replace the commas in the string with spaces.
main.py
my_int = 456789123 # ✅ format integer with space as the thousands separator result = f'{my_int:,}'.replace(',', ' ') print(result) # 👉️ 456 789 123 # --------------------------------- my_float = 456789123.456789 # ✅ format float with space as the thousands separator, rounded to 2 decimals result = f'{my_float:,.2f}'.replace(',', ' ') print(result) # 👉️ 456 789 123.46

The first step is to use a formatted string literal to format the number with a comma as the thousands separator.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_int = 456789123 result = f'{my_int:,}' print(result) # 👉️ 456,789,123

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

The comma after the colon is the thousands separator.

The last step is to use the str.replace() method to replace the commas in the string with spaces.

main.py
my_int = 456789123 result = f'{my_int:,}'.replace(',', ' ') print(result) # 👉️ 456 789 123

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

You can use the same approach if you need to format a floating-point number with a space as the thousands separator rounded to N decimal places.

main.py
my_float = 456789123.456789 result = f'{my_float:,.2f}'.replace(',', ' ') print(result) # 👉️ 456 789 123.46
The comma after the colon is the thousands separator and the digit after the period is the number of decimal places the float should have.

If you have the number of decimal places stored in a variable, wrap it in curly braces in the f-string.

main.py
my_float = 456789123.456789 number_of_decimals = 2 result = f'{my_float:,.{number_of_decimals}f}'.replace(',', ' ') print(result) # 👉️ 456 789 123.46

If you need to format a list of numbers with a space as the thousands separator, use a list comprehension.

main.py
my_list = [344531, 923482, 827345] result = [f'{item:,}'.replace(',', ' ') for item in my_list] print(result) # 👉️ ['344 531', '923 482', '827 345']

We used a list comprehension to iterate over the list of floating-point numbers.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use a formatted string literal and the str.replace() method to format the current number with a space as the thousands separator.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.