Python – Get the value of a string expression

In this post we will be creating a Python script that will retrieve the value of a string expression. To do this we will use the built in ‘eval()’ function which parses and evaluates a string expression as a Python expression.

See below our string for this example.

expression = "3+5"

See the snippet of Python code below that will retrieve the value of our string expression using the ‘eval()’ function.

expression = "3+5"

print(eval(expression))

The above would return the following as output.

8
>>> 

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply