Home »
Python »
Python Programs
Print number with commas as thousands separators in Python
Here, we are going to learn how to print number with commas as thousands separators in Python programming language.
By IncludeHelp Last updated : January 13, 2024
Many times, while writing the code we need to print the large number separated i.e. thousands separators with commas.
In python, such formatting is easy. Consider the below syntax to format a number with commas (thousands separators).
"{:,}".format(n)
Here, n is the number to be formatted.
Problem statement
Given a number n, we have to print it with commas as thousands separators.
Example
Consider the below example with sample input and output:
Input:
n = 1234567890
Output:
1,234,567,890
Printing a number using commas as thousands separators
To print a number using commas as thousands separators, use the .format() method with the combination of colon (:) and comma (,) enclosed inside the curly braces ({}) and pass the number inside the .format() method. The simple code statement for this "{:,}".format(n), where n is the number to be formatted.
Below is the user-defined function to print a number using commas as thousands separators:
def formattedNumber(n):
return ("{:,}".format(n))
Python program to print number with commas as thousands separators
# function to return number with
# thousands separators
def formattedNumber(n):
return "{:,}".format(n)
# Main code
print(formattedNumber(10))
print(formattedNumber(100))
print(formattedNumber(1000))
print(formattedNumber(10000))
print(formattedNumber(100000))
print(formattedNumber(1234567890))
print(formattedNumber(892887872878))
Output
The output of the above program is:
10
100
1,000
10,000
100,000
1,234,567,890
892,887,872,878
To understand the above program, you should have the basic knowledge of the following Python topics:
Python Basic Programs »