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?
Submitted by IncludeHelp, on April 27, 2020

What is a prime number?

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.

Given a number n, we have to print it with commas as thousands separators.

Example:

    Input:
    n = 1234567890
    
    Output:
    1,234,567,890

Python program to print number with commas as thousands separators in Python

# function to return number with thousand separator
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

10
100
1,000
10,000
100,000
1,234,567,890
892,887,872,878





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.