1

Result must include the delimiter.

Ex: a='1,000' of type string and the output must be: a=1,000 as an integer.

Praveen
  • 8,945
  • 4
  • 31
  • 49
sarath
  • 19
  • 2
  • 4
    If you want to use a delimiter like this in Py3.6, use the underscore, e.g. `int('1_000')`, see PEP 515 – Chris_Rands Sep 28 '18 at 08:45
  • For convenience the link: [PEP-515](https://www.python.org/dev/peps/pep-0515/) – colidyre Sep 28 '18 at 09:07
  • Possible duplicate of [How do I use Python to convert a string to a number if it has commas in it as thousands separators?](https://stackoverflow.com/questions/1779288/how-do-i-use-python-to-convert-a-string-to-a-number-if-it-has-commas-in-it-as-th) – mkrieger1 Sep 28 '18 at 10:12

1 Answers1

2

An integer won't include a comma. It's only for making the number readable that you add commas to it.

If you meant you want to parse the string into an integer, you should do the following:

num = int(a.replace(',', ''))

Afterwards if you want to present this integer with a comma again, you should just:

print "{:,}".format(num)

For back and fourth conversion in execution: Format and replace can help

Faisal Maqbool
  • 121
  • 1
  • 8
Ben Sh
  • 49
  • 6
  • 1
    This is useful if you have to stay on `,` as delimiter. Otherwise [Chris_Rands comment](https://stackoverflow.com/questions/52551510/how-can-i-save-numbers-with-comma-delimiter-stored-as-string-as-an-integer-in-py#comment92042027_52551510) is the way of doing it. – colidyre Sep 28 '18 at 09:05