What's the difference between `raw_input()` and `input()` in Python 3?

343,889

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
343,889
pkumar
Author by

pkumar

Updated on April 05, 2020

Comments

  • pkumar
    pkumar about 4 years

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

  • Martin Thoma
    Martin Thoma over 10 years
    You should add that Python 3 does not have raw_input().
  • ivan_pozdeev
    ivan_pozdeev about 9 years
    "What's the difference between raw_input...?" - "The difference is that there's no raw_input." ...Quite a drastic difference, I'd say!
  • Admin
    Admin about 9 years
    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.
  • AAM111
    AAM111 over 7 years
    Repl.it, running Py3.5.1 has raw_input() as a keyword.
  • Akshay Vijay Jain
    Akshay Vijay Jain over 6 years
    what is the use of running input as python expression?
  • MarSoft
    MarSoft over 6 years
    @AkshayVijayJain, probably it was intended for entering numbers. But it is totally unsafe.
  • PM 2Ring
    PM 2Ring over 6 years
    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.
  • Sven Marnach
    Sven Marnach over 6 years
    @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.
  • MJM
    MJM almost 6 years
    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#input