将字符串转换为可计算的操作 [英] Convert string to calculable operation
本文介绍了将字符串转换为可计算的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将字符串转换为可计算的操作
Is it possible to convert string to a calculable operation
我想做到这一点:
>>> import math
>>> operation = "10/2*6 + math.sqrt(42)"
>>> compute(operation)
36.48074069840786
推荐答案
eval
将为您做到这一点.
>>> import math
>>> operation = "10/2*6 + math.sqrt(42)"
>>> eval(operation)
36.48074069840786
这篇关于将字符串转换为可计算的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文