0

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'"
1
  • int("'2000'".strip("'") should do it. "TAT".strip("T") returns "A" - all the given characters to strip are removed from start and end of string Commented Nov 3, 2018 at 16:12

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

so how should i convert double quoted string to single string? because this is the one of the value given data set.
You get the string '2000' with t="'2000" as a result, so you want to omit the first and the last character of the string, leaving it as 2000. In python you can slice a string by taking a range of the string (by the braces []) with a from-to range as from character to character (i.e. [1:5] giving the secound to the sixt char of a string), you can also reverse index with -1 being the last char in the string, so t[1:-1] gives you all the string but the first and last char and converts the rest to string ^^
1

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.

Comments

0

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)

4 Comments

but this value is given me in my data set problem.so i can't change this my own.
@Bhaskararya Take a look at the answer from @paulwurtz, you can do int(t[1:-1]) with t being t = "'2000'" and it will work aswell.
Look on my answer, but for short: strip the string if received from else where int(t[1:-1])
can you tell me how to make double quoted string to single quoted string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.