0

I have to evaluate an expression, which uses operators that are stored in variables.

item1 = "*"
item2 = "+"
item3 = "/"
item4 = "-"

suppose I want to compute the value of 4*3-5/8, how could I do this with my string variables ?

user1251007
17k14 gold badges52 silver badges78 bronze badges
asked Jan 7, 2014 at 23:21
1
  • Read about eval. Commented Jan 7, 2014 at 23:33

1 Answer 1

1

Use eval:

>>> eval('5+4')
Out[15]: 9
>>> eval('5*4')
Out[16]: 20
>>> eval('5-4')
Out[17]: 1
answered Jan 7, 2014 at 23:54
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks BartoszKP and Steinar Lima, this what I was looking for :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.