-1

You may haven't understood the question correctly. So here is it in detailed way: There is a string, let's say, x='200+350'

so now if I do int(x), it'll give me an error. I want it to evaluate to 550 which is integer.

How may I do that?

2 Answers2

0

You can use eval()

x = '(2+3)*2'
print( eval(x) )

prints out 10

bvmcode
  • 2,182
  • 18
  • 32
0

try to use eval method

eval("200+350")
Sideeg MoHammed
  • 390
  • 2
  • 10
  • What if the string contains more operators like - ```"200+300*40"```? – The Emperor Sep 10 '20 at 11:06
  • As my answer states, that also works, which uses eval() like this answer – bvmcode Sep 10 '20 at 11:10
  • yah if it contains more operators will work fine but make sure the string is digits without any char >>>> happy for haring it help please make my answer accepted – Sideeg MoHammed Sep 10 '20 at 11:17
  • You should at least warn people that [eval is evil](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) when you suggest people use it. – khelwood Sep 10 '20 at 12:29