Reputation: 40
I need to convert a string into an int type, so i can perform my operation
>>> t="'2000'"
>>> int(t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: "'1'"
Upvotes: 0
Views: 80
Reputation: 5239
You're trying to parse the string '2000'
into an int, which it is not since there are quotes around the 2000
. You can change the code to this to make it work:
t = "'2000'"
t = t[1:-1]
int(t)
Upvotes: 0
Reputation: 1771
You have a double quoted string, in the error message it warns you about thr '
beeing a illegal number for conversion. Either clean your string to t="10"
removing the redundant quotes or strip the string if received from else where int(t[1:-1]).
Upvotes: 3
Reputation: 11
t
is a string with the value of '2000'
. You are looking to make t
a string with only the number 2000
by doing t = "2000"
. With int()
you are trying to convert the apostrophes to ints as well which you can't.
Upvotes: 1