How do I use raw_input in Python 3?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
raw_input() does not exist in Python 3 — it was renamed to input(). In Python 2, raw_input() returned user input as a string, while input() evaluated the input as a Python expression (equivalent to eval(raw_input())). Python 3 removed this dangerous eval behavior and made input() behave like Python 2's raw_input(), always returning a string. If you are porting Python 2 code to Python 3, replace every raw_input() call with input(). For code that needs to run on both versions, use a compatibility shim.
Python 2 vs Python 3 Comparison
Python 3's input() is always safe — it returns whatever the user types as a string, without evaluation.
Basic Usage of input() in Python 3
Type Conversion Patterns
Writing Code Compatible with Python 2 and 3
Reading Multiple Lines of Input
Input with Default Values
Secure Input (Passwords)
getpass.getpass() works like input() but suppresses the echo, making it suitable for password entry.
Common Pitfalls
- Using
raw_input()in Python 3: This raisesNameError: name 'raw_input' is not defined. Replace allraw_input()calls withinput(). Use find-and-replace or the2to3tool for automated migration. - Forgetting that
input()always returns a string:input("Age: ")returns"25"(a string), not25(an integer). Arithmetic likeinput() + 1raisesTypeError. Always cast explicitly withint(),float(), etc. - Using Python 2's
input()for user data: Python 2'sinput()callseval()on the result, which executes arbitrary code. If porting from Python 2, replaceinput()withraw_input()first, then rename toinput()for Python 3. Never useeval(input())in production code. - Not handling
ValueErroron type conversion:int(input())crashes if the user types non-numeric text. Always wrap conversions intry/except ValueErroror validate the input string before converting. input()blocking in scripts that read from pipes: When stdin is redirected (e.g.,echo "data" | python script.py),input()reads from the pipe, not the terminal. Usesys.stdin.isatty()to detect whether input is interactive, andsys.stdinfor line-by-line processing of piped data.
Summary
- Python 3 renamed
raw_input()toinput()— both return a string - Python 2's
input()(which eval'd the result) was removed for security - Always convert
input()results explicitly:int(input()),float(input()) - Use
try/except ValueErrorfor safe type conversion of user input - For Python 2/3 compatibility, use
try: input = raw_inputor thesixlibrary - Use
getpass.getpass()for password input that hides typed characters
Related reading
- How do I use sklearn CountVectorizer with both 'word' and 'char' analyzer? - python
- How do I use the group_by_window function in TensorFlow
- How do I use threading in Python?
- How do I use threading in Python?
- How do I use threading in Python?
- How do I validate a date string format in python?
- How do I write good/correct package __init__.py files
- How do I write JSON data to a file?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.