What is the difference between `raw_input()` and `input()` in Python 3?
In Python 2, there were two functions for getting user input:
raw_input(): Returned user input as a plain string.input(): Attempted to evaluate the user input as Python code (e.g., typing1+2would return3).
Starting with Python 3, raw_input() was removed, and input() was redefined to always return a string, making it equivalent to Python 2’s raw_input(). This change simplified user input handling in Python and removed the security risks associated with automatically evaluating inputs.
Key Points
-
Python 2:
raw_input()→ returns a string.input()→ evaluates the input as code (not recommended for untrusted input).
-
Python 3:
raw_input()→ No longer exists.input()→ Always returns a string (just likeraw_input()in Python 2).
Example (Python 3)
user_input = input("Enter something: ") print(user_input) # user_input is always a string.
Takeaway
- In Python 3,
input()is the only function for reading user input, and it always provides a string. - The old
raw_input()from Python 2 is gone because Python 3’sinput()does exactly whatraw_input()used to do.
If you come across code that uses raw_input(), it’s likely Python 2 code. To make that code run in Python 3, simply replace any raw_input() calls with input().
TAGS
Python
CONTRIBUTOR
TechGrind