Skip to content
Sign upLog in
← Back to Community

how to convert a string with numbers into an integer?

Profile icon
Egghead21

Line 44, Funcs.py is the problem
supposedly, it should remove the strings from the phrase to convert it into an integer.

Answered by AlexanderBirabe [earned 5 cycles]
View Answer
Voters
Profile icon
JoshuaFacey
Profile icon
ErnestMcClanaha
Profile icon
alexkagy
Profile icon
AlexanderTarn
Profile icon
Egghead21
Comments
hotnewtop
Profile icon
AlexanderBirabe
myInt = '' for i in list(myString): if i in '1234567890': myInt += i myInt = int(myInt)
Profile icon
Egghead21

okay, thanks!

Profile icon
AlexanderBirabe

If you think my post answered your question, mark it as the answer.

@Egghead21

Profile icon
Egghead21

@AlexanderBirabe
what is the myString thing do?
it isn't a list- so would it be myInt?

Profile icon
AlexanderBirabe

myString is the string that contains a number you want to isolate. myInt is the extracted number.


@Egghead21

Profile icon
Egghead21

@AlexanderBirabe
I'm confused- why would i wan't to isolate a number- do you mean the string part?

Profile icon
AlexanderBirabe

Your question is 'how to convert a string with numbers into an integer?' The code I gave you takes a string with a number (myString) and outputs an integer (myInt).
For example, if myString was "I am 20 years old", myInt would end up being the number 20.


@Egghead21

Profile icon
Egghead21

@Egghead21
I put myInt instead of myString, and it worked!
Thanks

Profile icon
AlexanderBirabe

Wait, are you just converting from a string straight to an integer?
If so, then just do myStr = int(myStr), which would convert the string myStr to it's integer form.
For example, if myStr was '10', then it would become the number 10.

@Egghead21