By pkumar


2011-02-06 18:52:34 8 Comments

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

7 comments

@Ing. Josef Klotzner 2019-02-15 16:01:15

If You want to ensure, that your code is running with python2 and python3, use function input () in your script and add this to begin 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")

@Sven Marnach 2011-02-06 18:53:25

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

@ivan_pozdeev 2015-02-10 21:03:05

"What's the difference between raw_input...?" - "The difference is that there's no raw_input." ...Quite a drastic difference, I'd say!

@J. C. Rocamonde 2015-04-03 13:56:30

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.

@OldBunny2800 2016-08-15 02:20:21

Repl.it, running Py3.5.1 has raw_input() as a keyword.

@PM 2Ring 2017-11-05 10:53:08

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 2017-11-05 12:33:11

@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 2018-07-24 11:00:00

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

@Sandeep_black 2018-10-22 09:10:35

Both are same, the only difference is to use them in their respective Python version.

In python2:

raw_input(): It takes exactly what user type and passes it back as string object.

input(): It takes exactly what used typed and then convert the type of entered object. Example. used entered [10,20,30], then it will return as List object type.

In Python3:

input(): It is exactly same as raw_input() in Python2.

eval(input()): It is exactly same as input() in Python2.

@Matt B. 2018-10-22 09:14:28

so what every single answer in this thread already says?? This question is like 8 years old..

@Rubal 2015-06-15 20:52:55

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.

@screenglow 2013-02-28 07:07:50

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

@askewchan 2013-09-29 22:39:56

... in python 2.

@Mike McMahon 2014-07-12 06:58:43

not applicable for python 3

@Harsha Vardhan 2015-12-30 16:01:45

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.

@Thomas K 2011-02-06 19:01:43

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.

@Martin Thoma 2013-11-05 09:28:24

You should add that Python 3 does not have raw_input().

@Akshay Vijay Jain 2017-08-11 05:52:09

what is the use of running input as python expression?

@MarSoft 2017-10-20 00:19:23

@AkshayVijayJain, probably it was intended for entering numbers. But it is totally unsafe.

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] What does if __name__ == "__main__": do?

40 Answered Questions

[SOLVED] What does the "yield" keyword do?

26 Answered Questions

[SOLVED] Difference between append vs. extend list methods in Python

23 Answered Questions

[SOLVED] What is the difference between @staticmethod and @classmethod?

44 Answered Questions

[SOLVED] How to merge two dictionaries in a single expression?

15 Answered Questions

[SOLVED] What are metaclasses in Python?

18 Answered Questions

[SOLVED] What's the difference between lists and tuples?

  • 2009-03-09 15:41:25
  • Lucas Gabriel Sánchez
  • 373723 View
  • 909 Score
  • 18 Answer
  • Tags:   python list tuples

7 Answered Questions

[SOLVED] Understanding Python super() with __init__() methods

6 Answered Questions

[SOLVED] What are the differences between type() and isinstance()?

21 Answered Questions

[SOLVED] Difference between __str__ and __repr__?

Sponsored Content