Arnaud Aliès
Arnaud Aliès

Reputation: 1092

Convert string to calculable operation

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

Upvotes: 0

Views: 191

Answers (1)

Keith Randall
Keith Randall

Reputation: 23265

eval will do that for you.

>>> import math
>>> operation = "10/2*6 + math.sqrt(42)"
>>> eval(operation)
36.48074069840786

Upvotes: 2

Related Questions