## Convert string to a statement

This is the place for queries that don't fit in any of the other categories.

### Convert string to a statement

Hi, everyone. So, suppose I have a string that goes like 'y = sin(x) + x'. After setting x to a value, how do I make Python evaluate the given string to as statement, to calculate the value of y? Thanks in advance!
stdq

Posts: 4
Joined: Tue Jul 23, 2013 10:04 pm

### Re: Convert string to a statement

Code: Select all
`>>> from math import sin>>> x = 2>>> y = sin(x) + x>>> print y2.90929742683`

Edit: ignor my irevelant piece of crap code, until i see micseydel's post i wondered why you where saying i have a string
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

Yoriz

Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

### Re: Convert string to a statement

I hope no one flames me, but I believe this is a good time for the responsible use of eval()*. The right hand side of the equation is something Python can handle, you just have to tell it to. I would recommend splitting the user's input on " = " and throwing away the left side, or better yet, simply prompt the user with "y = " so that you don't have to deal with that.

Once you're left with "sin(x) + x" and your own x variable, you just have to tell eval() to use the variables and to throw away powerful Python built-ins which aren't very secure (such as eval() itself). eval() takes three arguments, the last two optional but very important. The first is your string you want evaluated. The second and last specify what is available for eval() to use. They should include as little as possible typically, although including all of math is alright.

Code: Select all
`import mathuser_string = "sin(x) + x"scope = {} # start with NOTHINGscope.update(vars(math)) # put math stuff in therescope['x'] = math.pi # put your own x value in the scope for eval() to usey = eval(user_string, {}, scope) # (eval_string, globals, locals)print y`

I've done more work for you here than I normally do when answering questions, because using eval() in the recommended way carries a bit of baggage. You still do have some left to do though, and if you have questions about it remember to include your entire incorrect code, in code tags, along with the entire traceback if you get it, also in code tags. And of course, tell us what your input is, what your output is, and what your desired output is.

* eval() is potentially dangerous, and is discouraged from general use because of it even in instances where the user input is considered known, because historically such assumptions are found to be false. Above, I have outlined a secure way of using it, although you could get away with less and have your program work if this wasn't a concern.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Convert string to a statement

Thanks!
stdq

Posts: 4
Joined: Tue Jul 23, 2013 10:04 pm