number = int(input())
# input: 2345212
print(f"{number:,}") # using inbuilt python's separator
# output:
# Giving this : 2,345,212
# desired output: 23,45,212
I wish to generate correctly formatted numbers as output. But, I am not able to figure it out. As in desired output ( 23,45,212 ) from the last, it has a comma after 3 places thereafter a comma is placed on passing 2 digits. In India, it’s the standard form to place commas to digits.
Looking for the shortest trick.
[ Already tried locale. I want to print without currency( not this: ₹23,45,212.00 ) ]
>Solution :
You can do something like this using the built-in locale library:
import locale
number = int(input())
locale.setlocale(locale.LC_MONETARY, 'en_IN')
print(locale.currency(number, grouping=True))
Or without the currency symbol:
import locale
number = int(input())
locale.setlocale(locale.LC_MONETARY, 'en_IN')
print(locale.currency(number, grouping=True)[1:])