eval evaluates the python expression. In python 3, numbers starting by 0 aren't allowed (except for 0000, see Why does 000 evaluate to 0 in Python 3?). In python 2, those are interpreted as octal (base 8) numbers. Not better... (python 3 base 8 now uses exclusively Oo prefix)

int performs a string to integer conversion, so it cannot evaluate a complex expression (that you don't need), but isn't subjected to this leading zero syntax.

Another nice feature is that you can check if the entered expression is an integer by using a simple and qualified try/except block:

Copywhile True:
   try:
       age = int(input("enter age"))
       break
   except ValueError:
       print("Retry!")

(with eval you would have to protect against all exceptions)

Advice: use int, because it's safer, doesn't have security issues (eval can evaluate any expression, including system calls and file deletion), and suits your purpose perfectly.

Note: the above code is still unsafe with python 2: input acts like eval. You could protect your code against this with the simple code at the start of your module:

Copytry:
    input = raw_input
except NameError:
    pass

so python 2 input is not unreachable anymore and calls raw_input instead. Python 3 ignores that code.

Answer from Jean-François Fabre on Stack Overflow
Top answer
1 of 2
9

eval evaluates the python expression. In python 3, numbers starting by 0 aren't allowed (except for 0000, see Why does 000 evaluate to 0 in Python 3?). In python 2, those are interpreted as octal (base 8) numbers. Not better... (python 3 base 8 now uses exclusively Oo prefix)

int performs a string to integer conversion, so it cannot evaluate a complex expression (that you don't need), but isn't subjected to this leading zero syntax.

Another nice feature is that you can check if the entered expression is an integer by using a simple and qualified try/except block:

Copywhile True:
   try:
       age = int(input("enter age"))
       break
   except ValueError:
       print("Retry!")

(with eval you would have to protect against all exceptions)

Advice: use int, because it's safer, doesn't have security issues (eval can evaluate any expression, including system calls and file deletion), and suits your purpose perfectly.

Note: the above code is still unsafe with python 2: input acts like eval. You could protect your code against this with the simple code at the start of your module:

Copytry:
    input = raw_input
except NameError:
    pass

so python 2 input is not unreachable anymore and calls raw_input instead. Python 3 ignores that code.

2 of 2
6

eval() is used to verify an expression. On number is considered an expression, except octal numbers (numbers that start with 0). int() handles string to integer conversion. There are many reasons why you should avoid using eval(). Just keep in mind:

  • Python 2.x

    Copyx = raw_input('Enter number here: ') 
    
  • Python 3.x

    Copyx = input('Enter number here: ') 
    
  • Python 2.x
    Security risk:

    Copyx = input('Enter number here: ') 
    
  • Python 3.x
    Security risk:

    Copyx = eval(input('Enter number here: ')) 
    

Also, keep in mind that eval() has the potential to run code, which could cause a huge security risk. I suggest not using it unless you clearly know what you're doing or it could compromise your application.

🌐
YouTube
youtube.com › watch
Use of eval() in Python - Python eval function - int() vs eval() in Python - Python Programming - YouTube
We always use int () in python to covert string to integer. like x=int(input("Enter No 1:"))y=int(input("Enter No 2:"))z=x+yprint("Sum:",z)we can use eval()
Published   February 18, 2022
🌐
Sololearn
sololearn.com › en › discuss › 2532442 › what-is-the-difference-between-int-input-and-eval-input
What is the difference between int(input ()) and eval(input ()) ? | Sololearn: Learn to code for FREE!
October 6, 2020 - With int(input()) this will be ... string number to integer dtype.Instead eval will convert an operation done with numbers in string format to integer result so input will be integer.with int you can not perform operations like ...
🌐
php.cn
m.php.cn › home › backend development › python tutorial › the difference between eval and int in python
The difference between eval and int in python-Python Tutorial-php.cn
When eval() processes numbers, single and double quotation marks are interpreted as int type by the eval() function; triple quotation marks are interpreted as str type. For strings enclosed in eval() brackets, NameError will occur if the string ...
🌐
CopyProgramming
copyprogramming.com › howto › what-is-the-difference-between-eval-and-int
Python: Distinction between eval and int
April 24, 2023 - However, this code is ignored in Python 3. ... eval() serves the purpose of expression verification, with the exception of octal numbers (numbers beginning with 0). On the other hand, int() is responsible for converting strings to integers.
Find elsewhere
🌐
DataFlair
data-flair.training › blogs › python-eval-function
Python eval Function - Examples & Uses - DataFlair
August 27, 2018 - Here, we take the expression from the user, then we take the value of x and convert it to an integer. Finally, we call eval() on the expression and evaluate it to reach a value of 40. So this is useful, but it can also be used against us since it executes anything we pass to it(somewhat like SQL injection). Let’s see how. The user can: ... Imagine having a function in your code that returns some kind of password or other confidential data. A user can make a call to this function through Python eval() and make their way to the piece of sensitive data.
🌐
Towards Data Science
towardsdatascience.com › home › latest › python eval() built-in-function
Python eval() built-in-function | Towards Data Science
January 20, 2025 - Answer: eval is a built-in- function ... as a python expression. In simple words, the eval function evaluates the "String" like a python expression and returns the result as an integer....
🌐
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.
🌐
USAVPS
usavps.com › home › python › python tutorial: differences between int and eval functions in python
Python Tutorial: Differences between int and eval Functions in Python - USAVPS.COM
October 22, 2024 - Purpose: The int() function is used for type conversion to integers, while eval() is used for evaluating Python expressions from strings.
🌐
Tech mastery
technicalmasterblog.wordpress.com › 2016 › 10 › 18 › difference-between-raw_input-input-and-eval-in-python
Difference between raw_input, input and eval in python – Tech mastery
February 12, 2019 - In Python 2, raw_input() returns a string But whereas input() tries to run the input as a Python expression. Since getting a string was almost always what you wanted, Python 3 does that with input() . Note: input() is now raw_input() in python 3.x versions. eval in python Eval evaluates expressions. eval() , as the name suggests, evaluates the…
🌐
Real Python
realpython.com › python-eval-function
Python eval(): Evaluate Expressions Dynamically – Real Python
October 21, 2023 - Given that you don’t provide a custom dictionary to locals, the argument defaults to the dictionary passed to globals. In this case, eval() doesn’t have access to x because globals holds an empty dictionary. The main practical difference between globals and locals is that Python will automatically insert a "__builtins__" key into globals if that key doesn’t already exist.
🌐
GeeksforGeeks
geeksforgeeks.org › eval-in-python
eval in Python - GeeksforGeeks
July 15, 2024 - Here the eval statement x == 4 will evaluate to False because the value of x is 5, which is not equal to 4. In the second eval statement, x is None will evaluate to True because the value of x is None, and is None checks for object identity, not value equality. ... We can also evaluate condition checking on the Python eval() function.
🌐
GitHub
hplgit.github.io › primer.html › doc › pub › input › ._input-readable003.html
User input and error handling
is valid syntax, as this is how we initialize a string r in Python. To repeat, if we put the valid syntax 'math programming' inside a string, ... eval(s) will evaluate the text inside the double quotes as 'math programming', which yields a string. So, why is the eval function so useful? When we get input via raw_input or sys.argv, it is always in the form of a string object, which often must be converted to another type of object, usually an int or float.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › eval() function in python
Learn eval() Function in Python
June 11, 2025 - Try Our Free Online Compilers Choose your language, write your code, and Run code instantly: Python, Java & more. ... Python’s eval() function runs Python code stored as a string. It takes a string, treats it as a Python expression, and then calculates its value.
🌐
University of Utah
web.physics.utah.edu › ~detar › lessons › python › whoyou › node8.html
Making Python interpret the input: eval
Python's eval function interprets the character string and determines its type automatically. So if you enter the number 25, the eval function converts it to an integer, assigns it to the variable age and gives age the type int.