2

I just start learning python, and would like to ask the reader to input a number,but i would like to skip comma and space that reader input.

a = input("input a number:")
x = y
print(dec(x))

However, if i use split, it would become a list or 2 number for example, when user input 12,3456, and y would become ['12', '3456']

And my expected output is 123456 as a integer but not a list with two values.

I tried to use replace before, but it said

"TypeError: object of type 'int' has no len()"

Underoos
  • 4,708
  • 8
  • 42
  • 85
KI TO
  • 61
  • 4
  • Could you please provide a [minimal reproducible example](/help/mcve)? – norok2 Dec 14 '19 at 09:25
  • 1
    You could use *str.replace* to get rid of each of the unwanted characters. – CristiFati Dec 14 '19 at 09:28
  • Does this answer your question? [Python parse comma-separated number into int](https://stackoverflow.com/questions/2953746/python-parse-comma-separated-number-into-int) – CristiFati Dec 14 '19 at 09:35

2 Answers2

2

Instead of using split, you can just use replace to remove any comma or space from the string you read from input.

a=input("input a number:")
a = a.replace(",","").replace(" ","")
print(a)
Thiago
  • 694
  • 3
  • 12
  • 26
1

You could try something like this.

>>> number = int("".join(input().split(',')))
12,3456
>>> number
123456
>>> 

Basically just splitting the input based on ',' and then joining them

You could also try replacing the ',' with ''

>>> number = int(input().replace(',',''))
12,3456
>>> number
123456
>>>

Hope this helps!

Anurag A S
  • 725
  • 10
  • 23
  • I tried and it said that TypeError: object of type 'int' has no len(), how can i solve it? – KI TO Dec 14 '19 at 09:33