0

For example, I have a


String = "TGR1UK"

I only need the number 1 in the string to do some math operation so I make a number variable as


number = String[3]

but the type of this "number" is actually Char and I cant use

number = (int)String[3] 

to cast it into an integer

what should I do?

1
  • 1
    There is no char type in python. And I'm pretty sure number = (int)String[3] would raise a syntax error. Why aren't you calling the int function? It looks like you are trying to do a C-like cast. Python is not C, there are no casts Commented Oct 16, 2021 at 17:13

2 Answers 2

1
number = int(String[3])

This will cast it to an int. Read more here:

https://careerkarma.com/blog/python-string-to-int/

Edit: I have assumed when you said:

but the type of this "number" is actually Char and I cant use number = (int)String[3]

That you meant that wasnt working, because that is not how you cast to an int in python. Did you mean you aren't allowed to use a straight cast for some reason?

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

2 Comments

Thank you! I just simply misplaced my curly brackets lol
Those aren't curly brackets btw, these are: {}. Those were parentheses or, simply, brackets
0

You're using int wrong. int is used as follows:

int(string)

So, try number = int(String[3]).

1 Comment

Yes, that's the cause, Thank you for answering!

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.