Last updated: Apr 9, 2024
Reading timeยท5 min
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.
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
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.
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
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.
You can also use the str.format()
method to format a number with a comma as
the thousands separator.
my_int = 489985123 result = '{:,}'.format(my_int) print(result) # ๐๏ธ 489,985,123 result = '{:,}'.format(my_int).replace(',', ' ') print(result) # ๐๏ธ 489 985 123
The str.format() method performs string formatting operations.
Here is an example that does that in a locale-aware manner.
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
The same approach can be used to format a number with thousands separator to 2 decimal places.
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
We used a formatted string literal to format numbers with a comma as the thousands separator to N decimals.
f
.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.
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
If you have the number of decimal places stored in a variable, wrap it in curly braces in the f-string.
my_float = 489985.456789 number_of_decimals = 2 result = f'{my_float:,.{number_of_decimals}f}' print(result) # ๐๏ธ 489,985.46
If you need to format a list of integers with a comma as the thousands separator, use a list comprehension.
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)
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.
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.
If you need to format a list of floating-point numbers with a comma as the thousands separator, use a list comprehension.
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']
We used a list comprehension to iterate over the list of floating-point numbers.
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.
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.
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.
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
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.
If you need to format the number as currency without a comma as the thousands separator, remove the comma from the f-string.
my_float = 15467.3 result = f'${my_float:.2f}' print(result) # ๐๏ธ $15467.30
Alternatively, you can use the locale.currency()
method.
To format a float as currency:
locale.setlocale()
method to set the locale to en_US.UTF-8
.locale.currency()
method to format the float as currency.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
We used the
locale.setlocale()
method to set the locale to en_US.UTF-8
.
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.
symbol
keyword argument is set to True
.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.
You can learn more about the related topics by checking out the following tutorials: