Here I need to evaluate an expression present within string.
for eg,
>> a = '5 + 6'
>> a = 5 + 6
How to achieve this?
2 Answers 2
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
Mihai Alexandru-Ionut
48.6k14 gold badges106 silver badges132 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Open AI - Opting Out
Any provisos about safety?
You can use eval() as it treats string as code.
a = '5 + 6'
eval(a)
Comments
Explore related questions
See similar questions with these tags.
lang-py