0

Is it possible to convert string to a calculable operation

I want to make this done:

>>> import math
>>> operation = "10/2*6 + math.sqrt(42)"
>>> compute(operation)
36.48074069840786
tshepang
  • 11,360
  • 21
  • 88
  • 132
Arnaud Aliès
  • 1,079
  • 13
  • 26

1 Answers1

2

eval will do that for you.

>>> import math
>>> operation = "10/2*6 + math.sqrt(42)"
>>> eval(operation)
36.48074069840786
Keith Randall
  • 22,753
  • 1
  • 33
  • 54