What's the difference between raw_input and input in Python 3?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
This question is really about the difference between Python 2 and Python 3, not about two different Python 3 APIs. In Python 3, raw_input does not exist anymore. The Python 3 input() function behaves like Python 2 raw_input(), which is why old tutorials and migrated code often cause confusion.
Python 2 Had Two Different Functions
In Python 2, console input had two common functions:
- '
raw_input()returned text as a string,' - '
input()evaluated the entered text as a Python expression.'
That distinction mattered a lot.
If the user typed hello into Python 2 input(), Python tried to evaluate hello like a variable name, which often caused an error. If the user typed 2 + 3, Python evaluated it and returned 5.
That evaluation behavior made Python 2 input() risky and confusing.
Python 3 Simplified The Model
Python 3 removed raw_input() and kept only input().
In Python 3, input() always returns a string. It does not evaluate the user’s text as Python code. That means Python 3 input() is conceptually the replacement for Python 2 raw_input().
So the practical migration rule is simple:
- Python 2
raw_input()becomes Python 3input().
Convert Explicitly When You Need A Number
Because Python 3 input() returns text, numeric conversion is your responsibility.
This design is clearer because the code says exactly what it expects. You are not relying on hidden expression evaluation.
You can also write it directly:
That is the usual modern pattern.
Why Python 2 input() Was Problematic
The Python 2 version of input() effectively used evaluation semantics, which meant unexpected or malicious input could do far more than simple text entry.
That is why Python 3 removed the distinction and made input() return plain text. It is safer and easier to reason about.
A beginner no longer has to memorize a hidden rule like "one input function returns a string, the other runs Python code." The language now has one consistent interface.
A Migration Example
Suppose old Python 2 code looked like this:
A Python 3 rewrite should be explicit about types:
This is clearer than trying to preserve Python 2 behavior exactly.
What Happens If You Call raw_input In Python 3
If you try to run old code containing raw_input() in Python 3, you get a NameError because the function is gone.
That is a migration signal, not a subtle runtime behavior change.
Input Is Always Text First
A useful mental model is: console input arrives as text first. If you need an integer, float, date, list, or custom object, parse it intentionally.
That keeps type conversion visible and avoids surprising behavior.
Common Pitfalls
- Expecting
raw_input()to exist in Python 3. - Assuming Python 3
input()automatically returns integers or other non-string types. - Porting Python 2 code and forgetting to replace
input()evaluation behavior with explicit parsing. - Calling
int(input(...))without handling invalid numeric input. - Reading old Python 2 examples and applying them directly to modern Python 3 code.
Summary
- Python 3 has
input(), but notraw_input(). - Python 3
input()behaves like Python 2raw_input(). - Python 2
input()evaluated user text as a Python expression; Python 3 does not. - Convert the returned string explicitly with
int(),float(), or other parsers when needed. - When migrating old code, replace
raw_input()withinput()and make type conversion explicit.
Related reading
- What's the difference between scikit-learn and tensorflow? Is it possible to use them together?
- What's the difference between select_related and prefetch_related in Django ORM?
- What''s the difference between str.isdigit, isnumeric and isdecimal in Python?
- What's the difference between subprocess Popen and call how can I use them?
- What's the difference between tf.cond and if-else?
- What's the difference between tf.expand_dims and tf.newaxis in Tensorflow?
- What's the difference between using Dataset and ndarray in fit method in Tensorflow 2?
- What's the function like sum but for multiplication? product?
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.