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
  • 12,111
  • 21
  • 91
  • 136
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,985
  • 2
  • 35
  • 54