Print number with commas as 1000 separators in Python

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.