Python Reference Library →

Python eval() Function – Evaluate Expressions from Strings

Let’s look at eval() function – evaluate expressions from strings

  1. Notice

    eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
expression = "3 + 2"
result = eval(expression)
print(result)
Live state

Top-level scope

expression→Object 1
result→Object 2
Object 1 · str'3 + 2'Object 2 · int5
This step

eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.

Picture eval() function – evaluate expressions from strings

eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.

Image brief

Alt text
A diagram for Python eval() Function – Evaluate Expressions from Strings. Instructions: expression = "3 + 2" result = eval(expression) print(result). Displayed result: 5. The final example bindings are expression --> object 1 (str); result --> object 2 (int). object 1: '3 + 2'; object 2: 5
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python eval() Function – Evaluate Expressions from Strings". Teaching focus: eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS expression = "3 + 2" result = eval(expression) print(result) | v DISPLAYED RESULT 5 FINAL BINDINGS expression --> object 1 (str) result --> object 2 (int) object 1: '3 + 2' object 2: 5 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python eval() Function – Evaluate Expressions from Strings. Instructions: expression = "3 + 2" result = eval(expression) print(result). Displayed result: 5. The final example bindings are expression --> object 1 (str); result --> object 2 (int). object 1: '3 + 2'; object 2: 5

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
expression = "3 + 2"
result = eval(expression)
print(result)
Live state

Top-level scope

expression→Object 1
result→Object 2
Object 1 · str'3 + 2'Object 2 · int5
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
5

What do you expect this program to print?

What does this example display?

expression = "3 + 2"
result = eval(expression)
print(result)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

5

Follow the printed values in order. The output is: 5

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.

eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.

More examples and reference

Python eval() Function

The eval() function in Python takes a string and evaluates it as a Python expression. It can be used to run dynamic Python code that's stored as a string.

Syntax

eval(expression, globals=None, locals=None)

Parameters:

  • expression – A string that contains a valid Python expression.
  • globals (optional) – A dictionary to define the global namespace.
  • locals (optional) – A dictionary to define the local namespace.

Returns:

  • The result of evaluating the expression (could be a number, string, list, etc.).

Example 1: Evaluate a Mathematical Expression

result = eval("5 + 10 * 2")
print(result)
25

Example 2: Use Variables in eval()

x = 3
y = 4
expression = "x * y + 2"
print(eval(expression))
14

Example 3: Using globals and locals

code = "a + b"
print(eval(code, {"a": 5, "b": 10}))
15

Use Case: When to Use eval()

  • Useful in calculators, config parsers, or dynamic logic evaluators.
  • Can execute user-defined expressions at runtime.

Warning: Security Risk

Never use eval() on untrusted input! It can execute arbitrary code, leading to serious security issues.

Example of dangerous input:

eval("__import__('os').system('rm -rf /')")

Instead, use ast.literal_eval() if you just want to safely evaluate basic data types.

Common Mistakes

  • Invalid syntax inside the string will raise a SyntaxError.
  • Runtime errors like division by zero can still occur inside eval.

Interview Tip

While eval() isn’t commonly used in interviews, understanding it shows that you know Python’s dynamic features and their risks.

Summary

  • eval() evaluates a string as a Python expression.
  • It can return different data types based on the expression.
  • Use it with caution — it can be unsafe with user inputs.

Practice Problem

Ask the user to enter a math expression (like 2 + 3 * 4) and print the result using eval():

expr = input("Enter a math expression: ")
print("Result:", eval(expr))

What to remember

Connect what you noticed in the example with the idea below.

Remember

eval() evaluates a Python expression and returns its value. Only authored literal expressions are shown here; untrusted text must not be evaluated.

Watch out for this idea

eval() is a safe way to process arbitrary untrusted user input.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →