Why does this happen?

x = eval(input("Enter a number: ")) is not the same thing as x = eval('input("Enter a number: ")')

The former first calls input(...), gets a string, e.g. '5' then evaluates it, that's why you get an int, in this manner:

>>> eval('5') # the str '5' is e.g. the value it gets after calling input(...)
5 # You get an int

While the latter (more aligned with what you were expecting), evaluates the expression 'input("Enter a number: ")'

>>> x = eval('input("Enter a number: ")')
Enter a number: 5
>>> x 
'5' # Here you get a str
Answer from bakkal on Stack Overflow
🌐
Real Python
realpython.com › python-eval-function
Python eval(): Evaluate Expressions Dynamically – Real Python
October 21, 2023 - You can use the built-in Python eval() to dynamically evaluate expressions from a string-based or compiled-code-based input. If you pass in a string to eval(), then the function parses it, compiles it to bytecode, and evaluates it as a Python ...
🌐
W3Schools
w3schools.com › python › ref_func_eval.asp
Python eval() Function
Python Examples Python Compiler ... Python Training ... The eval() function evaluates the specified expression, if the expression is a legal Python statement, it will be executed....
People also ask

Is eval a keyword in python?
Yes, `eval` is a built-in function in Python, not a keyword. It is used to evaluate a string as a Python expression and return the result.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › eval in python
Eval Function () in Python: Usage, Examples & Security Considerations
What is eval ()?
Evaluating or running an argument is what the eval() function does. Eval() evaluates an expression if the parameter is one. Eval() evaluates the arguments if they contain one or more JavaScript statements.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › eval in python
Eval Function () in Python: Usage, Examples & Security Considerations
What are the eval's parameters?
Expression, globals, and locals are the three arguments required by the eval() method.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › eval in python
Eval Function () in Python: Usage, Examples & Security Considerations
🌐
GeeksforGeeks
geeksforgeeks.org › python › eval-in-python
eval in Python - GeeksforGeeks
July 23, 2025 - Python eval function comes with the facility of explicitly passing a list of functions or variables that it can access. We need to pass it as an argument in the form of a dictionary.
🌐
Programiz
programiz.com › python-programming › methods › built-in › eval
Python eval()
The eval() method parses the expression passed to this method and runs python expression (code) within the program.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › eval() function in python
Learn eval() Function in Python
June 11, 2025 - The eval() function parses the expression passed to it and evaluates it as a Python expression. In other words, it takes a string input, interprets it as a Python expression, and executes it within the program.
Find elsewhere
🌐
Python Geeks
pythongeeks.org › python geeks › learn python › python eval() function with examples
Python eval() Function with Examples - Python Geeks
October 30, 2021 - In this article, you will be learning ... introduction. The function eval() is a built-in function that takes an expression as an input and returns the result of the expression on evaluation....
🌐
Codecademy
codecademy.com › docs › python › built-in functions › eval()
Python | Built-in Functions | eval() | Codecademy
July 11, 2025 - The eval() function is a built-in Python function that dynamically evaluates and executes Python expressions from string-based input.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › eval in python
Eval Function () in Python: Usage, Examples & Security Considerations
March 16, 2026 - Careless use of eval() can open the door to denial of service attacks. An attacker can create an expression that consumes excessive resources or goes into an infinite loop. user_input = input("Enter a Python expression: ") result = eval(user_input)
🌐
Python Programming
pythonprogramming.net › python-eval-tutorial
Eval Module with Python Tutorial
First, we define a string, that carries the syntax of a list. Next, we use eval to evaluate it. Finally, we can show that it has the properties of a Python list. x = input("see this...") check_this_out = eval(input("code:"))
🌐
Leanware
leanware.co › insights › how-to-use-eval-in-python
How to Use eval() in Python with Examples & Best Practices
Automated Trading Platform Dev for Algorithmic Trading Co
This makes it useful for dynamic expression evaluation, but also dangerous if you pass untrusted input.Most Python developers encounter eval() when building calculators, processing configuration files, or working with dynamic code generation. The function does exactly what it promises, which ... Leanware has a good working process and iterates on the product quickly, adapting to the client’s methodologies. The team is accommodating with the timeline and communicates effectively through thrice-weekly phone calls, Slack, and reports. Their flexibility has made them an excellent partner. Learn ho
Rating: 5 ​
🌐
GitHub
hplgit.github.io › primer.html › doc › pub › input › ._input-readable003.html
User input and error handling
The program code itself has no knowledge about the kind of function the user wants to work with, yet at run time the user's desired formula enters the computations. The eval functions takes a string as argument and evaluates this string as a Python expression.
🌐
University of Utah
web.physics.utah.edu › ~detar › lessons › python › whoyou › node8.html
Making Python interpret the input: eval
For example, when you input your age, we saw that the result was treated as a string of characters. To get Python to treat it as an integer constant, you would do this: Python's eval function interprets the character string and determines its type automatically.
🌐
Scaler
scaler.com › home › topics › eval() in python
eval() in Python - Scaler Topics
June 1, 2022 - Now when you input a string as input in the function, eval() in python first parses the expression that you gave, then compiles it into bytecode. It is then evaluated as a python expression and the result of that is returned.
🌐
Python Central
pythoncentral.io › how-to-use-pythons-eval-function
How to Use Python's eval() Function | Python Central
May 16, 2025 - Without proper restrictions, eval() can access system functions: # DANGEROUS - Grants access to system operations user_input = "__import__('os').system('rm -rf /')" eval(user_input) # Could attempt to delete system files!
🌐
Towards Data Science
towardsdatascience.com › home › latest › python eval() built-in-function
Python eval() built-in-function | Towards Data Science
January 20, 2025 - Also if the user wants to evaluate ... because eval function evaluates the string expression and returns the integer as a result. Now you all know the input() takes the user input, but when the user enters an integer as an input the input function returns a string, but in the case of eval it will evaluate the returned value from a string to an integer...
🌐
Real Python
realpython.com › ref › builtin-functions › eval
eval() | Python’s Built-in Functions – Real Python
The built-in eval() function allows you to dynamically evaluate Python expressions from a string or compiled code object and returns the result of the evaluated expression:
🌐
Medium
medium.com › @jamescog72 › pythons-eval-function-and-how-to-use-it-securely-e9f0594f605c
Python’s eval() function and how to use it securely. | by Yaekob Demisse | Medium
December 23, 2022 - This means that you can pass it a string containing a mathematical expression, and eval() will return the result of that expression. You can also pass it a string containing a valid Python statement, such as a function definition or a for loop, and eval() will execute that code. One common use case for eval() is to evaluate user input in a program.