What's the difference between `raw_input()` and `input()` in Python 3? [duplicate]
Asked Answered
B

6

399

What is the difference between raw_input() and input() in Python 3?

Berryman answered 6/2, 2011 at 18:52 Comment(4)
How do you make a program that takes input both Python 2 and Python 3 compatible?Dyal
To do that you try to set input to raw_input and ignore name errors.Dyal
Look up the 'six' library for python 2 and 3 compatibility.Oblong
What I do is just writing Python 2 code in "try:" block and Python 3 code in "except NameError:" block.Icehouse
C
452

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.)

Clapperclaw answered 6/2, 2011 at 18:53 Comment(6)
"What's the difference between raw_input...?" - "The difference is that there's no raw_input." ...Quite a drastic difference, I'd say!Divers
In Python 2 I guess they assumed programmers wanted to actually "execute" as a command the user input, since initially (I guess) requesting input from user might only be for that. But when they realised programmers might also want to get the "raw" input, they designed another function called "raw_input". In Python 3 they noticed what stupid was that and simply deleted the original input default's execution, yielding only one simple function.Larvicide
Repl.it, running Py3.5.1 has raw_input() as a keyword.Staford
It needs to be said that eval (and exec) should generally be avoided because they can be a security risk. For details, please see Eval really is dangerous by SO veteran Ned Batchelder. And of course that advice also applies to the old Python 2 input.Stonehenge
@PM2Ring I added a warning to the answer. Of course there are valid use cases for both eval() and exec(), but you first need to understand why you shouldn't use eval() before deciding to use it.Clapperclaw
It's worth noting that this is still being tweaked, the docs show that Py34 and Py36 have seen changes in base int handling: [link]docs.python.org/3/library/functions.html#inputWording
L
199

In Python 2, raw_input() returns a string, and 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(). As Sven says, if you ever want the old behaviour, eval(input()) works.

Lippmann answered 6/2, 2011 at 19:1 Comment(3)
You should add that Python 3 does not have raw_input().Orvil
what is the use of running input as python expression?Permenter
@AkshayVijayJain, probably it was intended for entering numbers. But it is totally unsafe.Aurel
M
125

Python 2:

  • raw_input() takes exactly what the user typed and passes it back as a string.

  • input() first takes the raw_input() and then performs an eval() on it as well.

The main difference is that input() expects a syntactically correct python statement where raw_input() does not.

Python 3:

  • raw_input() was renamed to input() so now input() returns the exact string.
  • Old input() was removed.

If you want to use the old input(), meaning you need to evaluate a user input as a python statement, you have to do it manually by using eval(input()).

Marcelline answered 28/2, 2013 at 7:7 Comment(0)
Y
31

In Python 3, raw_input() doesn't exist which was already mentioned by Sven.

In Python 2, the input() function evaluates your input.

Example:

name = input("what is your name ?")
what is your name ?harsha

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    name = input("what is your name ?")
  File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined

In the example above, Python 2.x is trying to evaluate harsha as a variable rather than a string. To avoid that, we can use double quotes around our input like "harsha":

>>> name = input("what is your name?")
what is your name?"harsha"
>>> print(name)
harsha

raw_input()

The raw_input()` function doesn't evaluate, it will just read whatever you enter.

Example:

name = raw_input("what is your name ?")
what is your name ?harsha
>>> name
'harsha'

Example:

 name = eval(raw_input("what is your name?"))
what is your name?harsha

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    name = eval(raw_input("what is your name?"))
  File "<string>", line 1, in <module>
NameError: name 'harsha' is not defined

In example above, I was just trying to evaluate the user input with the eval function.

Yoheaveho answered 30/12, 2015 at 16:1 Comment(0)
O
8

I'd like to add a little more detail to the explanation provided by everyone for the python 2 users. raw_input(), which, by now, you know that evaluates what ever data the user enters as a string. This means that python doesn't try to even understand the entered data again. All it will consider is that the entered data will be string, whether or not it is an actual string or int or anything.

While input() on the other hand tries to understand the data entered by the user. So the input like helloworld would even show the error as 'helloworld is undefined'.

In conclusion, for python 2, to enter a string too you need to enter it like 'helloworld' which is the common structure used in python to use strings.

Origami answered 15/6, 2015 at 20:52 Comment(0)
K
5

If You want to ensure, that your code is running with python2 and python3, add input() function at the beginning of your script:

from sys import version_info
if version_info.major == 3:
    pass
elif version_info.major == 2:
    try:
        input = raw_input
    except NameError:
        pass
else:
    print ("Unknown python version - input function not safe")
Kemp answered 15/2, 2019 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.