Format number with comma as thousands separator in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Formatting a Number with a comma as the thousands separator
  2. Format number with thousands separator to 2 decimals
  3. Formatting a List of Integers with a comma as the thousands separator
  4. Formatting a List of Floats with a Comma as thousands separator
  5. Format a float as currency in Python

# Formatting a Number with a comma as the thousands separator

Use a formatted string literal to format a number with a comma as the thousands separator.

You can use an expression in the f-string to format the number with a comma as the thousands separator and optionally rounded to N decimals.

main.py
my_int = 489985123 # โœ… Format integer with commas as thousands separator result = f'{my_int:,}' print(result) # ๐Ÿ‘‰๏ธ 489,985,123 # โœ… Format integer with spaces as thousands separator result = f'{my_int:,}'.replace(',', ' ') print(result) # ๐Ÿ‘‰๏ธ 489 985 123

formatting number with comma as thousands separator

The code for this article is available on GitHub

To format an integer with a comma as the thousands separator, we just use a comma after the colon in the expression.

You can also use the locale module to set the locale-aware string formatting.

main.py
import locale locale.setlocale(locale.LC_ALL, '') my_int = 489985123 result = f'{my_int:n}' print(result) # ๐Ÿ‘‰๏ธ 489,985,123 result = f'{my_int:n}'.replace(',', ' ') print(result) # ๐Ÿ‘‰๏ธ 489 985 123

using the locale module to set the locale aware string formatting

The locale.setlocale() method modifies the locale setting.

When the locale.LC_ALL attribute is set to an empty string, the user's preferred locale is used.

As noted in the docs, the , option signals the use of a comma for a thousands separator.

The n integer presentation type is used for a locale-aware separator.

# Formatting a Number with a comma as the thousands separator using str.format()

You can also use the str.format() method to format a number with a comma as the thousands separator.

main.py
my_int = 489985123 result = '{:,}'.format(my_int) print(result) # ๐Ÿ‘‰๏ธ 489,985,123 result = '{:,}'.format(my_int).replace(',', ' ') print(result) # ๐Ÿ‘‰๏ธ 489 985 123

formatting number with comma as thousands separator using str format

The code for this article is available on GitHub

The str.format() method performs string formatting operations.

Here is an example that does that in a locale-aware manner.

main.py
import locale locale.setlocale(locale.LC_ALL, '') my_int = 489985123 result = '{:n}'.format(my_int) print(result) # ๐Ÿ‘‰๏ธ 489,985,123 result = '{:n}'.format(my_int).replace(',', ' ') print(result) # ๐Ÿ‘‰๏ธ 489 985 123

# Format number with thousands separator to 2 decimals

The same approach can be used to format a number with thousands separator to 2 decimal places.

main.py
my_float = 489985.456789 # โœ… Format a number with comma as thousands separator rounded to 2 decimals result = f'{my_float:,.2f}' print(result) # ๐Ÿ‘‰๏ธ 489,985.46 # โœ… Format a number with comma as thousands separator rounded to 3 decimals result = f'{my_float:,.3f}' print(result) # ๐Ÿ‘‰๏ธ 489,985.457
The code for this article is available on GitHub

We used a formatted string literal to format numbers with a comma as the thousands separator to N decimals.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐Ÿ‘‰๏ธ is subscribed: True

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.

main.py
my_float = 489985.456789 print(f'{my_float:,.2f}') # ๐Ÿ‘‰๏ธ 489,985.46 print(f'{my_float:,.3f}') # ๐Ÿ‘‰๏ธ 489,985.457 # --------------------------------------------- my_integer = 456789123 print(f'{my_integer:,}') # ๐Ÿ‘‰๏ธ 456,789,123
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 = 489985.456789 number_of_decimals = 2 result = f'{my_float:,.{number_of_decimals}f}' print(result) # ๐Ÿ‘‰๏ธ 489,985.46

# Formatting a List of Integers with a comma as the thousands separator

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

main.py
my_list = [489985123, 123456789, 23456789, 45678901] a_list = [f'{x:,}' for x in my_list] # ๐Ÿ‘‡๏ธ ['489,985,123', '123,456,789', '23,456,789', '45,678,901'] print(a_list)
The code for this article is available on GitHub

On each iteration, we use an f-string to format the current number with a comma as the thousands separator and return the result.

The same approach can be used if you need to format a list of integers with a space as the thousands separator.

main.py
a_list = [f'{x:,}'.replace(',', ' ') for x in my_list] # ๐Ÿ‘‡๏ธ ['489 985 123', '123 456 789', '23 456 789', '45 678 901'] print(a_list)

On each iteration, we use the str.replace() method to replace each space with a comma in the output.

# Formatting a List of Floats with a Comma as thousands separator

If you need to format a list of floating-point numbers with a comma as the thousands separator, use a list comprehension.

main.py
list_of_floats = [489985.456789, 123456.678123, 234567.12345] result = [f'{item:,.2f}' for item in list_of_floats] print(result) # ๐Ÿ‘‰๏ธ ['489,985.46', '123,456.68', '234,567.12']
The code for this article is available on GitHub

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 to format the current float with a comma as the thousands separator to 2 decimal places and return the result.

The same approach can be used if you need to format a list of integers with a comma as the thousands separator.

main.py
list_of_numbers = [456789, 234567, 123456] result = [f'{item:,}' for item in list_of_numbers] print(result) # ๐Ÿ‘‰๏ธ ['456,789', '234,567', '123,456']

On each iteration, we use a formatted string literal to format the current number with a comma as the thousands separator and return the result.

# Format a float as currency in Python

You can also use a formatted-string literal to format a float as currency.

You can use an expression in the f-string to format the number with a comma as the thousands separator, rounded to 2 decimal places.

main.py
my_float = 15467.3 # โœ… Format a float as currency result = f'${my_float:,.2f}' print(result) # ๐Ÿ‘‰๏ธ $15,467.30 # โœ… Format a float as currency without a comma as thousands separator result = f'${my_float:.2f}' print(result) # ๐Ÿ‘‰๏ธ $15467.30
The code for this article is available on GitHub

We used a formatted string literal to format a number as currency.

The f-string starts with a dollar sign but you can remove it if it doesn't suit your use case.

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 need to format the number as currency without a comma as the thousands separator, remove the comma from the f-string.

main.py
my_float = 15467.3 result = f'${my_float:.2f}' print(result) # ๐Ÿ‘‰๏ธ $15467.30

Alternatively, you can use the locale.currency() method.

# Format a float as currency using locale.currency()

To format a float as currency:

  1. Use the locale.setlocale() method to set the locale to en_US.UTF-8.
  2. Use the locale.currency() method to format the float as currency.
main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') my_float = 15467.3 result = locale.currency(my_float, grouping=True) print(result) # ๐Ÿ‘‰๏ธ $15,467.30 result = locale.currency(my_float, grouping=False) print(result) # ๐Ÿ‘‰๏ธ $15467.30
The code for this article is available on GitHub

We used the locale.setlocale() method to set the locale to en_US.UTF-8.

main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

The setlocale() method takes the category and the locale.

The locale.LC_ALL category sets the locale for all categories.

The locale.currency() method formats the number according to the current settings.

The returned string includes the currency symbol if the symbol keyword argument is set to True.
main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') my_float = 15467.3 result = locale.currency(my_float, grouping=True, symbol=True) print(result) # ๐Ÿ‘‰๏ธ $15,467.30 result = locale.currency(my_float, grouping=False, symbol=False) print(result) # ๐Ÿ‘‰๏ธ 15467.30

You can set the grouping keyword argument to False to remove the comma thousands separator.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev