I'm looking for an easy way to convert a String s
, e.g. s = "x+1.0"
, into a term I can use as a mathematical term. The goal is to find the y-values for some x-values in a specific space.
So for s = "x+1.0"
I would get 6
for x=5
I'm looking for an easy way to convert a String s
, e.g. s = "x+1.0"
, into a term I can use as a mathematical term. The goal is to find the y-values for some x-values in a specific space.
So for s = "x+1.0"
I would get 6
for x=5
It sounds like you are just trying to convert a string into a Python expression and evaluate it.
You can do this with eval
, with all the usual warnings.
x = 5
s = eval("x+1.0") # Now s = 6.
You should use the eval
keyword: it allows the string to be evaluated as an expression.
x = 2
s = "x+1.0"
k = eval(s)
print k
Output: 3.0