Python
raw_input
input
Python 3
programming

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.

Browse interview questions

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.

python
# Python 2 behavior
name = raw_input("Name: ")
number = input("Number: ")

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().

python
name = input("Name: ")
print(name)
print(type(name))

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 3 input().

Convert Explicitly When You Need A Number

Because Python 3 input() returns text, numeric conversion is your responsibility.

python
age_text = input("Age: ")
age = int(age_text)
print(age + 1)

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:

python
age = int(input("Age: "))
print(age)

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:

python
# Python 2 style
username = raw_input("Username: ")
score = input("Score: ")

A Python 3 rewrite should be explicit about types:

python
1username = input("Username: ")
2score = int(input("Score: "))
3
4print(username, score)

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.

python
raw_input("Name: ")

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.

python
price = float(input("Price: "))
print(price * 1.2)

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 not raw_input().
  • Python 3 input() behaves like Python 2 raw_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() with input() and make type conversion explicit.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.