How to convert string type value into int type value in python 3?

786 views python-3.x
-2

** I need to convert string into 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'"

answered question

3 Answers

13

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"
int(t)

posted this
2

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.

posted this
2

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

posted this

Have an answer?

JD

Please login first before posting an answer.