Skip to content
Related Articles

Related Articles

Print number with commas as 1000 separators in Python

Improve Article
Save Article
  • Last Updated : 21 Jul, 2022
Improve Article
Save Article

In this program, we need to print the output of a given integer in international place value format and put commas at the appropriate place, from the right. Let’s see an example of how to print numbers with commas as thousands of separators in Python.

Examples

Input : 1000000
Output : 1,000,000

Input : 1000
Output : 1,00

Method 1: using f-string

F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting. The inner part within the curly brackets : , says to format the number and use commas as thousand separators.

Example 1:

Format the number and add commas as a thousand separators to use the ‘,d’ formatting syntax in the format() function.

Python3




num = 1000000000
 
print(f"{num:,d}")

Output

1,000,000,000

Example 2:

F-string with replaces function.

Python3




res = f'{10000000:,}'.replace('.',',')
print(res)

Output

10,000,000

Method 2: string.format()

Here, we have used the “{:,}” along with the format() function to add commas every thousand places starting from left. This is introduced in Python and it automatically adds a comma on writing the following syntax. 

Example 1:

Numbers with an integer.

Python3




res = '{:,}'.format(1000000)
print(res)

Output

1,000,000

Example 2:

Numbers with decimal.

Python3




num = 123456.1234567
 
res = '{:,.2f}'.format(num)
print(res)

Output

123,456.12

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!