Add Commas to Number in Python | In the article, we will learn how to add commas between numbers. So, let’s see how to print numbers with commas.
For example, a simple number can be written as 123456789 but it is difficult to read them. To improve the readability of the given number we can add commas to them. After adding commas the number will become 123,456,789.
Following are the different ways to add commas to number in Python:-
- Using f-string in Python
- Using string.format() Function
Add Commas to Number in Python Using f-string
F-strings provide a fortunate way to insert python expressions inside string literals for formatting.
num = 1000000
print(f"{num:,d}")
Output:-
1,000,000
Adding Commas To Numbers In Python Using string.format() Function
Here, we will use the “{:,}” along with the format() function to add commas every thousand places starting from left.
res = '{:,}'.format(123456789)
print(res)
Output:-
123,456,789
So, here, we have discussed both methods to print commas between numbers greater than 999. Hope you all enjoy the post and find it useful.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!