What's the difference between `raw_input()` and `input()` in Python 3?
The difference is that raw_input()
does not exist in Python 3.x, while input()
does. Actually, the old raw_input()
has been renamed to input()
, and the old input()
is gone, but can easily be simulated by using eval(input())
. (Remember that eval()
is evil. Try to use safer ways of parsing your input if possible.)
Differences between `input` and `raw_input`
In python 2.x, raw_input()
returns a string and input()
evaluates the input in the execution context in which it is called
>>> x = input()
"hello"
>>> y = input()
x + " world"
>>> y
'hello world'
In python 3.x, input
has been scrapped and the function previously known as raw_input
is now input
. So you have to manually call compile
and than eval
if you want the old functionality.
python2.x python3.x
raw_input() --------------> input()
input() -------------------> eval(input())
In 3.x, the above session goes like this
>>> x = eval(input())
'hello'
>>> y = eval(input())
x + ' world'
>>> y
'hello world'
>>>
So you were probably getting an error at the interpretor because you weren't putting quotes around your input. This is necessary because it's evaluated. Where you getting a name error?
Is it ever useful to use Python's input over raw_input?
Is it ever useful to use Python 2's input over raw_input?
No.
input()
evaluates the code the user gives it. It puts the full power of Python in the hands of the user. With generator expressions/list comprehensions, __import__
, and the if/else
operators, literally anything Python can do can be achieved with a single expression. Malicious users can use input()
to remove files (__import__('os').remove('precious_file')
), monkeypatch the rest of the program (setattr(__import__('__main__'), 'function', lambda:42)
), ... anything.
A normal user won't need to use all the advanced functionality. If you don't need expressions, use ast.literal_eval(raw_input())
– the literal_eval
function is safe.
If you're writing for advanced users, give them a better way to input code. Plugins, user modules, etc. – something with the full Python syntax, not just the functionality.
If you're absolutely sure you know what you're doing, say eval(raw_input())
. The eval
screams "I'm dangerous!" to the trained eye. But, odds are you won't ever need this.
input()
was one of the old design mistakes that Python 3 is solving.
How do I use raw_input in Python 3?
Starting with Python 3, raw_input()
was renamed to input()
.
From What’s New In Python 3.0, Builtins section second item.
Difference of input() in python2/3
Data type of answer is string, After changing it to int it will work fine
import random
import sys
#display the python version
print(sys.version)
#computer selects two random numbers between 1 and 10
x = random.randint(1,10)
y = random.randint(1,10)
#print x and y values
print("For x =", x, "and y =", y)
#calculate the correct answer to the sum of the two numbers and store it in the variable named "correct"
correct = x + y
#ask the user what is the sum of the two random numbers and provide a response
answer = input("What is the sum of x plus y ? ")
if(int(answer)==correct):
print("well done")
else:
print("Incorrect. The correct answer is", correct)
Differences between input commands in Python 2.x and 3.x
In Python 3.x, raw_input
became input
and Python 2.x's input
was removed. So, by doing this in 3.x:
text = input('Text here')
you are basically doing this in 2.x:
text = raw_input('Text here')
Doing this in 3.x:
text = eval(input('Text here'))
is the same as doing this in 2.x:
text = input('Text here')
Here is a quick summary from the Python Docs:
PEP 3111:
raw_input()
was renamed toinput()
. That is, the newinput()
function reads a line fromsys.stdin
and returns it with the trailing
newline stripped. It raisesEOFError
if the input is terminated
prematurely. To get the old behavior ofinput()
, useeval(input())
.
raw_input function in Python
It presents a prompt to the user (the optional arg
of raw_input([arg])
), gets input from the user and returns the data input by the user in a string. See the docs for raw_input()
.
Example:
name = raw_input("What is your name? ")
print "Hello, %s." % name
This differs from input()
in that the latter tries to interpret the input given by the user; it is usually best to avoid input()
and to stick with raw_input()
and custom parsing/conversion code.
Note: This is for Python 2.x
Related Topics
Why Can Tuples Contain Mutable Items
Creating a Dynamic Choice Field
Converting JSON String to Dictionary Not List
Python Referencing Old Ssl Version
What Does Preceding a String Literal with "R" Mean
Python:List Index Out of Range Error While Iteratively Popping Elements
Django Auto_Now and Auto_Now_Add
How to Install Both Python 2.X and Python 3.X in Windows
":=" Syntax and Assignment Expressions: What and Why
How to Install Python MySQLdb Module Using Pip
Selenium Give File Name When Downloading
Convert Floats to Ints in Pandas
Changing the Formatting of a Datetime Axis in Matplotlib
How to Avoid "Runtimeerror: Dictionary Changed Size During Iteration" Error