This is because, In python 2, raw_input()
accepts everything given to stdin, as a string, where as input()
preserves the data type of the given argument (i.e. if the given argument is of type int
, then it will remain as int
only, but won't be converted to string
as in case of raw_input()
). That is basically, when input()
is used, it takes the arguments provided in stdin as string, and evaluates the same. And this evaluation converts the argument to corresponding type.
# Python 2.7.6
>>> a = raw_input("enter :- ")
enter :- 3
>>> type(a) # raw_input() converts your int to string
<type 'str'>
>>> a = input("enter :- ")
enter :- 3
>>> type(a) # input() preserves the original type, no conversion
<type 'int'>
>>>
Therefore, while using input()
in Python 2, user has to be careful while passing the arguments. If you are passing a string, you need to pass it with quote ( since python recognizes characters inside quote as string). Else NameError
will be thrown.
>>> a = input("enter name :- ")
enter name :- Derrick
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'Derrick' is not defined
>>> a = input("enter name :- ")
enter name :- 'Derrick'
>>> a
'Derrick'
Whereas, if using raw_input()
, you need not worry about the data type while passing the argument as everything it accepts as a string. But yes, inside your code you need to take care of appropriate type conversion.
To avoid this extra care needed for input()
in Python 2, it has been removed in Python 3. And raw_input()
has been renamed to input()
in Python 3. The functionality of input()
from Python 2 is no more in Python 3. input()
in Python 3 serves what raw_input()
was serving in Python 2.
This post might be helpful for a detailed understanding.