0
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 ) ]

pythonbae
  • 55
  • 6
  • 1
    Does this answer your question? [Convert an amount to Indian Notation in Python](https://stackoverflow.com/questions/40951552/convert-an-amount-to-indian-notation-in-python) – Passerby Jan 17 '22 at 02:04
  • You need to use the `locale` module to define your locale ("en-IN", maybe). – Tim Roberts Jan 17 '22 at 02:07
  • That is printing the currency as well: ₹23,45,212.00 – pythonbae Jan 17 '22 at 02:16
  • Your edit does nothing to address the close reason. The duplicate has several answers which do not involve `locale` at all. – tripleee Jan 24 '22 at 18:16

1 Answers1

0

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:])
krmogi
  • 2,279
  • 1
  • 8
  • 25