Skip to content
Related Articles

Related Articles

Improve Article

Print number with commas as 1000 separators in Python

  • Last Updated : 05 Nov, 2017
Geek Week

In this program, we need to print output of a given integer in international place value format and put commas at the appropriate place, from the right.

Examples:

Input : 1000000
Output : 1,000,000

Input : 1000
Output : 1,000

Using {} along with format() function is introduced in Python 2.7 and is generally used in String formatting in place of “%”.
Syntax

" ".format()

Example:



Input  : print ('{0}, {1}, {2}'.format('a', 'b', 'c'))
Output : a, b, c

Input :  print ('{2}, {0}, {1}'.format('a', 'b', 'c'))
Output : c, a, b

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




def place_value(number):
    return ("{:,}".format(number))
  
print(place_value(1000000))

Output:

1,000,000

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :