-2

I have a string with a formula 5 - 3, and I need to get the result in integer. How could I do that?

2 Answers2

1

use eval function:

eval("5 - 3") # 2
ᴀʀᴍᴀɴ
  • 4,443
  • 8
  • 37
  • 57
  • Works like a charm! Thanks! – Vilius Povilaika Mar 07 '16 at 19:51
  • 1
    @ViliusPovilaika , Ok , but you should search SO first and then ask question , there is hunders of questions in SO , maybe similar one to your question founded! – ᴀʀᴍᴀɴ Mar 07 '16 at 19:53
  • Well I tried to look it up on SE and SO and came up with nothing. I guess I just don't know how to search right. – Vilius Povilaika Mar 07 '16 at 19:54
  • @ViliusPovilaika , try using similar key words for search , as you see duplicate question is very similar with your title , search more dude! – ᴀʀᴍᴀɴ Mar 07 '16 at 19:55
  • 1
    @Arman is right i just typed Convert a string equation to an integer answer and find an answer on [SO](http://stackoverflow.com/questions/19959333/convert-a-string-equation-to-an-integer-answer) – Mr Rubix Mar 07 '16 at 19:57
0
test = "5-3"
print(eval(test))

Gives 2

mptevsion
  • 937
  • 8
  • 28