0

Here I need to evaluate an expression present within string.

for eg,

>> a = '5 + 6'
>> a = 5 + 6

How to achieve this?

asked May 22, 2018 at 18:39
0

2 Answers 2

1

You could use eval method by passing an expression as argument. The expression argument is parsed and evaluated as a Python expression.

a = eval('5 + 6')
answered May 22, 2018 at 18:42
Sign up to request clarification or add additional context in comments.

1 Comment

Any provisos about safety?
0

You can use eval() as it treats string as code.

a = '5 + 6'

eval(a)

answered May 22, 2018 at 18:46

Comments

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.