What is the difference between raw_input() and input() in python3.x ?

1 upvote
  flag
How do you make a program that takes input both Python 2 and Python 3 compatible? – Solomon Ucko
1 upvote
  flag
To do that you try to set input to raw_input and ignore name errors. – Solomon Ucko
1 upvote
  flag
Look up the 'six' library for python 2 and 3 compatibility. – Jack Homan

5 Answers 11

up vote 314 down vote accepted

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, so if try to use safer ways of parsing your input if possible.)

52 upvote
  flag
"What's the difference between raw_input...?" - "The difference is that there's no raw_input." ...Quite a drastic difference, I'd say! – ivan_pozdeev
7 upvote
  flag
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. – J. C. Rocamonde
1 upvote
  flag
Repl.it, running Py3.5.1 has raw_input() as a keyword. – OldBunny2800
1 upvote
  flag
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. – PM 2Ring
1 upvote
  flag
@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. – Sven Marnach

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.

5 upvote
  flag
You should add that Python 3 does not have raw_input(). – Martin Thoma
upvote
  flag
what is the use of running input as python expression? – Akshay Vijay Jain
1 upvote
  flag
@AkshayVijayJain, probably it was intended for entering numbers. But it is totally unsafe. – MarSoft

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

11 upvote
  flag
... in python 2. – askewchan
1 upvote
  flag
not applicable for python 3 – Mike McMahon

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.

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.