What’s the Real Difference Between Python input() and raw_input()?
This article explains how Python input() behaves in Python 2 versus Python 3, compares it with raw_input(), shows the function syntax, provides concrete examples of reading strings and numbers, and reveals the simple one‑line implementation used in Python 2.
In Python 3 the input() function reads a line from standard input and always returns a str object. In Python 2, input() is equivalent to eval(raw_input(prompt)), meaning it evaluates the entered text as a Python expression. raw_input() (Python 2) always returns the entered data as a string, regardless of its content. By contrast, Python 2’s input() attempts to evaluate the input; if the user types a pure number it is returned as an int or float, but non‑numeric text must be quoted or a SyntaxError is raised.
The function signature is input([prompt]), where the optional prompt argument is displayed to the user before reading input.
Example in Python 3:
a = input("Please input your favorite number: ")
# User types 10
# a is the string '10'
b = input("Please input your name: ")
# User types Test
# b is the string 'Test'Because input() now behaves like the old raw_input(), any numeric input must be explicitly converted, e.g., int(a), if a numeric type is required.
In Python 2 the built‑in input can be expressed in a single line:
def input(prompt):
return eval(raw_input(prompt))Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
