I am informing myself with the functions to interact with the user. I'm with the basics: raw_input()
e input()
. I have read that it input()
only takes integer data, that it does not accept strings, and that for this we use raw_input()
.
The problem is that if I store a string in a variable with input()
if it accepts it and recognizes it. But if I try to do the same with raw_input()
it gives me the following error:
Traceback (most recent call last): File "lllll.py", line 9, in <module> raw_input() NameError: name 'raw_input' is not defined
I work with Python 3.5.2
The error occurs, as you have been told, because
raw_input
it does not exist inPython 3.X
. This has been and will continue to be a source of confusion because the functionsinput()
do not do the samePython 2
thing inPython 3
despite having the same name.In
Python 2.x
both functions, we are going to clarify a little what each one does:Python 2.x :
raw_input()
Returns a line of user input as is, raw and always returns a character string , an objectstr
(ASCII) containing the input.But remember that the variable
v
does not contain a number, it is not aint
, it is a string of characters (text). If we try to operate as an integer:We get an error telling us that we cannot add a string with an integer:
To make it an integer, you have to convert it by doing an explicit casting (Python has strong typing, it never does an implicit casting to adapt the type of the variable to the context):
input()
expects valid Python expressions to be passed to it . That is, only Python code can be passed to it, and the function evaluates and processes it as such.What happened here? Well, it
input()
has taken the expression and since it is valid Python code, it has evaluated it and has performed the operations. We could think that itinput()
only accepts numbers but this is false, I don't know why it has become so widespread, to the point of generalizing the idea that itinput
is for entering scalars andraw_input
for strings. Accept any valid expression in Python:Concatenating two strings:
Creating a list using list compression:
We can pass a string with no problem, but a string is passed to it using quotes, just as we declare a string literal in code:
Another very common mistake is to try to use
input
in Python 2 as it is used in Python 3, with the idea of assigning the string entered by the user to a variable:We have an error:
exactly the same as if we try to use a variable that is not defined before we do:
If we have understood what it does
input
in Python 2, it should not surprise us, when evaluating the string 'Jogofus' entered, it tries to look for the nameJogofus
in the global namespace (as if it were the name of a variable, a function, a class, etc).Python 3.x:
input()
does the same asraw_input()
inPython 2.x
. Reads a line of input from standard input and returns it raw in an objectstr
(UTF-8). In fact, it's the same function but renamed, the only difference is that in Python 2str
it's ASCII while in Python 3 it's a Unicode (UTF-8) string.The former
input()
is roughly equivalent toeval()
, which takes a string of text and tries to evaluate it as if it were valid Python code. The difference is that instead of reading the string from standard input as you doinput
in Python 2, you must pass it as an argument:In short, if you use
Python 3.x
only existsinput()
and always returns a string. If you usePython 2.x
raw_input
does the same thing asinput()
inPython 3.x
(accepts any string and always returns an objectstr
) whileinput()
only accepting expressions that are syntactically valid in Python (otherwise it will throw an error) and evaluating them afterwards.I advise you that when you look at documentation or tutorials you look at which version they use because the changes from branch 2 to 3 were important. Here is a list of the most significant:
https://docs.python.org/3/whatsnew/3.0.html
The function
raw_input()
was renamed toinput()
in Python 3.xThe old
input()
one can be emulated witheval(input())
For python2 there are 2 functions
input()
andraw_input()
, the first one is for numeric values, while the second one is for text strings; but this changes in python3 , theraw_input()
has been renamed to input(), and the old oneinput()
has been removed. So if you use python 3 you should useinput()
instead ofraw_input()