[Tutor] What's the difference between raw_input and input?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Sep 20 20:42:46 CEST 2004



On Mon, 20 Sep 2004, Olavi Ivask wrote:

> input simply gets raw input, sends it to eval, and then returns the
> result.  Raw_input returns input in unprocessed form.


Yes.  As an example:

###
>>> input_result = input()
42
>>> raw_input_result = raw_input()
42
>>> input_result
42
>>> raw_input_result
'42'
###


Note that there are quotes around raw_input_result: that's a hint that
it's actually a string, not just a number:

###
>>> type(input_result)
<type 'int'>
>>> type(raw_input_result)
<type 'str'>
###


There's some kind of interpretation that happens with input().  In fact,
stuff that's input()ted has access to all of Python:

###
>>> input()
3 * 4 ** 5
3072
>>> raw_input()
3 * 4 ** 5
'3 * 4 ** 5'
###


This is what Olavi means about eval(): It's actually "evaluating" what's
being entered: it uses Python itself to interpret the expression.  So
input() and raw_input() are significantly different in their effects.

I hardly use input() because it is a bit too powerful.  When I prompt the
user for something in my own programs, I usually want to control the
interpretation of their answer: I don't want Python getting at it first.



More information about the Tutor mailing list