10

In a tutorial, I read that that there is a difference between input and raw_input. I discovered that they changed the behavior of these functions in the Python 3.0. What is the new behavior?

And why in the python console interpreter this

x = input()

Sends an error but if I put it in a file.py and run it, it does not?

Kevin
  • 74,910
  • 12
  • 133
  • 166
Guillermo Siliceo Trueba
  • 4,251
  • 5
  • 34
  • 47

3 Answers3

19

In python 2.x, raw_input() returns a string and input() evaluates the input in the execution context in which it is called

>>> x = input()
"hello"
>>> y = input()
x + " world"
>>> y
'hello world'

In python 3.x, input has been scrapped and the function previously known as raw_input is now input. So you have to manually call compile and than eval if you want the old functionality.

python2.x                    python3.x

raw_input()   --------------> input()               
input()  -------------------> eval(input())     

In 3.x, the above session goes like this

>>> x = eval(input())
'hello'
>>> y = eval(input())
x + ' world'
>>> y
'hello world'
>>> 

So you were probably getting an error at the interpretor because you weren't putting quotes around your input. This is necessary because it's evaluated. Where you getting a name error?

aaronasterling
  • 68,820
  • 20
  • 127
  • 125
  • Whats going on here i tried your lines on the console (3.x) and it interpreted x = input() "hello" <-- i type this on the console when asked) x + " world" y 'x + "world" Why? x is not translated into a string – Guillermo Siliceo Trueba Sep 27 '10 at 03:19
  • the error i was getting >>> x = input() Traceback (most recent call last): File "", line 1, in EOFError: EOF when reading a line – Guillermo Siliceo Trueba Sep 27 '10 at 03:21
  • @Guillermo, The interpreter section I showed is for 2.x. I messed up on 3.x (i haven't played with it much at all) so I deleted that part of my answer. – aaronasterling Sep 27 '10 at 03:28
  • Mmm maybe i should start learning python 2.X i just wanted to have the latest with most future learning. but i don't want to get stuck like this as is a such a waste of time, i guess i could find another tutorial. – Guillermo Siliceo Trueba Sep 27 '10 at 03:31
  • @Guillermo, that's my final answer. Also, there's nothing that's a waste of time in the processes of learning. It turns out that I did have my understanding of 3.x right, I just tried to do something different in it. Why do you think you're wasting your time? – aaronasterling Sep 27 '10 at 03:43
  • I mean i don't want to learn something that is on its way of being depreceated like python 2.x. i think i'm just trying to learn the most efficient way. Thanks for your time, i tried the code you used and it worked as it should, the problem with the interpreter was that i'm using the notepad++ python plugin console and it does weird stuff i won't mess with it anymore i'll stick with the default one. – Guillermo Siliceo Trueba Sep 27 '10 at 03:52
3

Its simple:

  1. raw_input() returns string values
  2. while input() return integer values

For Example:

1.

x = raw_input("Enter some value = ")
print x

Output:

Enter some value = 123
'123'

2.

y = input("Enter some value = ") 
print y

Output:

Enter some value = 123
123

Hence if we perform x + x = It will output as 123123

while if we perform y + y = It will output as 246

shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • Maybe on python 2.x, but not on python 3.x. input() in python 3.x always return a string. Moreover, raw_input() has been deleted from python 3 – Stefano Munarini Sep 04 '15 at 16:06
3

input() vs raw_input()

raw_input collects the characters the user types and presents them as a string. input() doesn't just evaluate numbers but rather treats any input as Python code and tries to execute it. Knowledgeable but malicious user could type in a Python command that can even deleted a file. Stick to raw_input() and convert the string into the data type you need using Python's built in conversion functions.

Also input(), is not safe from user errors! It expects a valid Python expression as input; if the input is not syntactically valid, a SyntaxError will be raised.

Blueice
  • 143
  • 9