Ok, so i use a lot of input commands, and I understood that in Python2 I can do:
text = raw_input ('Text here')
But now that i use Python 3 I was wondering what's the difference between:
text = input('Text here')
and:
text = eval(input('Text here'))
when do I have to use the one or the other?
In Python 3.x, raw_input
became input
and Python 2.x's input
was removed. So, by doing this in 3.x:
text = input('Text here')
you are basically doing this in 2.x:
text = raw_input('Text here')
Doing this in 3.x:
text = eval(input('Text here'))
is the same as doing this in 2.x:
text = input('Text here')
Here is a quick summary from the Python Docs:
PEP 3111: raw_input()
was renamed to input()
. That is, the new input()
function reads a line from sys.stdin
and returns it with the trailing
newline stripped. It raises EOFError
if the input is terminated
prematurely. To get the old behavior of input()
, use eval(input())
.
These are equivalent:
raw_input('Text here') # Python 2
input('Text here') # Python 3
And these are equivalent:
input('Text here') # Python 2
eval(raw_input('Text here')) # Python 2
eval(input('Text here')) # Python 3
Notice that in Python 3 there isn't a function called raw_input()
, as Python's 3 input()
is just raw_input()
renamed. And in Python 3 there isn't a direct equivalent of Python 2's input()
, but it can be easily simulated like this: eval(input('Text here'))
.
Now, in Python 3 the difference between input('Text here')
and eval(input('Text here'))
is that the former returns the string representation of the input entered (with trailing newline removed), whereas the latter unsafely evaluates the input, as if it were an expression entered directly in the interactive interpreter.