Python input() vs raw_input(): Key Differences Explained

When I build a small command-line reporting script, I often need a quick way to ask the user for a file name, report month, or sales target. Python’s input functions make that easy, but code copied from an older tutorial can create confusing errors.

The biggest confusion comes from Python 2 and Python 3 handling user input differently. A script using raw_input() may work perfectly in an old environment and fail immediately on a modern machine.

This comparison guide shows exactly how Python input() vs raw_input() works, why the difference matters, and how to write safe user-input code today.

Python input() vs raw_input() at a Glance

Here is the short answer: use input() in Python 3. Use raw_input() only when you must maintain a legacy Python 2 script.

FeaturePython 2 input()Python 2 raw_input()Python 3 input()
ReturnsEvaluated Python expressionText stringText string
Accepts 25Integer 25String "25"String "25"
Accepts 2 + 3Integer 5String "2 + 3"String "2 + 3"
Security riskHighLowerLower
Available in Python 3NoNoYes
Recommended todayNoOnly for old codeYes

Python 2 reached end of life long ago, so new scripts, automation tools, data utilities, and web applications should target Python 3. If you are unsure which version runs on your machine, start with this guide on Python 3 vs Python 2.

What Does Python input() Do?

The input() function pauses a program, displays an optional prompt, and waits for the user to type a value. In Python 3, it always returns that value as a string, which means text inside quotes.

Here is a basic example from a simple report-export script:

report_name = input("Enter the report name: ")
print(f"Creating report: {report_name}")

If the user enters April Sales, Python stores "April Sales" in report_name. The f before the second string creates an f-string, which lets you place a variable directly inside text.

You can use the same pattern to collect a folder name, email subject, project code, or other text value. For a closer look at prompts and returned values, see how to use the Python input function.

input() Returns Text, Even for Numbers

This detail catches many beginners. When a user types 12, Python 3 stores "12", not the number 12.

days_text = input("How many days should the report cover? ")
print(days_text * 2)

If the user enters 12, the output is:

1212

Python repeats the string because days_text is text. Convert the value with int() when your script needs a whole number.

days = int(input("How many days should the report cover? "))
print(days * 2)

I executed the above example code and added the screenshot below.

Python input() and raw_input()

Now an entry of 12 produces 24. This conversion matters in automation scripts because calculations, loops, and comparisons need numeric data. You can also review how to convert a string to an integer in Python for more examples.

How raw_input() Worked in Python 2

In Python 2, raw_input() collected user input as a string. Its behavior matches Python 3’s input().

# Python 2 only
client_name = raw_input("Enter the client name: ")
print("Preparing report for: " + client_name)

If the user enters Contoso, client_name holds the string "Contoso". That predictable behavior made raw_input() the safer choice for normal text entry in Python 2.

A Python 2 script could convert the returned text when needed:

# Python 2 only
record_count = int(raw_input("Enter the number of records: "))
print(record_count + 10)

The script asks for text first, then explicitly converts it to an integer. That clear two-step pattern remains the best approach in Python 3.

Pro Tip: In my experience, explicitly converting input after reading it prevents more bugs than trying to make user input “smart.” Read text first, validate it, then convert it only when the program needs a number.

Why Python 2 input() Was Risky

Python 2’s input() did not simply collect text. It evaluated what the user typed as Python code.

# Python 2 only — do not use this pattern
value = input("Enter a value: ")
print(value)

If the user entered 10, Python returned the integer 10. If they entered 5 + 5, Python calculated the expression and returned 10.

That behavior sounds convenient, but it creates a major security problem. A user could enter code that performs unwanted actions on the computer. Never use Python 2 input() in a real script that accepts input from anyone else.

This behavior also made scripts unpredictable. A plain name such as April caused an error because Python tried to treat it as a variable instead of text.

Python input() vs raw_input() in Real Scripts

The key difference is not only the function name. It is what Python does with the entered value.

Python 2 Example

# Python 2
file_name = raw_input("Enter a CSV file name: ")
row_limit = int(raw_input("Enter the maximum rows: "))

print("File:", file_name)
print("Rows:", row_limit)

This script safely collects both values as text. It then converts only row_limit, because the script needs a number for processing.

Python 3 Equivalent

# Python 3
file_name = input("Enter a CSV file name: ").strip()
row_limit = int(input("Enter the maximum rows: "))

print(f"File: {file_name}")
print(f"Rows: {row_limit}")

The .strip() method removes accidental spaces before and after the file name. I use it often in local scripts because pasted input frequently includes a trailing space.

If your script reads a CSV after collecting the file name, this Python Pandas CSV guide is a useful next step.

Handle Invalid Input Properly

Users do not always enter what you expect. Someone may press Enter without typing anything, enter ten instead of 10, or paste a value with extra spaces. Good exception handling keeps the script from crashing.

while True:
try:
report_months = int(input("Enter report months (1-12): "))

if 1 <= report_months <= 12:
break

print("Enter a number from 1 to 12.")

except ValueError:
print("Please enter a whole number.")

print(f"Generating a {report_months}-month report.")

I executed the above example code and added the screenshot below.

Python input() vs raw_input()

The while True loop keeps asking until the user provides an acceptable value. The try block contains code that might fail, while except ValueError handles invalid numeric text such as three.

This approach works well in command-line applications, scheduled automation helpers, and data-cleaning scripts where user input controls a date range or record limit.

Pro Tip: I always validate values near the point where I collect them. If invalid data reaches deeper parts of a script, debugging becomes slower and error messages become less useful.

Migrating raw_input() Code to Python 3

Moving an old Python 2 script to Python 3 is usually straightforward. Replace each raw_input() call with input().

Before: Python 2

department = raw_input("Enter department: ")
budget = float(raw_input("Enter budget: "))

After: Python 3

department = input("Enter department: ").strip()
budget = float(input("Enter budget: "))

The result stays the same because Python 3 input() returns a string just like Python 2 raw_input(). Test every conversion carefully, especially int() and float(), because old scripts may assume different types.

Also check print statements, division behavior, and third-party packages when upgrading. The raw_input() function in Python article can help when you need to understand older code before replacing it.

Things to Keep in Mind

  • Use Python 3 for new projects: Write input(), not raw_input(), because modern Python does not include raw_input().
  • Validate numeric entries: Wrap int() and float() conversions in try/except blocks to handle invalid text cleanly.
  • Treat input as untrusted: Never evaluate text entered by users with eval() or a Python 2-style input() pattern.
  • Remove unwanted whitespace: Use .strip() for names, file paths, IDs, and menu choices where surrounding spaces cause problems.
  • Show clear prompts: Tell users the expected format, such as Enter month count (1-12):, instead of using vague prompts.
  • Keep conversions explicit: Read input as text, then convert it only where a calculation or comparison requires a number.

Frequently Asked Questions

What is the difference between input() and raw_input() in Python?

In Python 3, input() returns a string. In Python 2, raw_input() returns a string, while input() evaluates the entered text as Python code. For modern projects, use Python 3 input().

Does raw_input() work in Python 3?

No. Python 3 does not include raw_input(). Running it produces a NameError; replace it with input().

Why does Python input() return a string?

Python receives keyboard input as text. Returning a string keeps the function safe and predictable, then lets you choose whether to convert the value to an integer, float, date, or another type.

How do I take an integer input in Python 3?

Wrap input() with int():
pythonage = int(input("Enter your age: "))
Use try/except ValueError if users might enter invalid values.

Is Python 2 input() safe to use?

No. Python 2 input() evaluates user input as code, which can create serious security risks. Use raw_input() in legacy Python 2 scripts, then convert the result yourself.

How do I fix “name raw_input is not defined”?

You are likely running Python 2 code in Python 3. Replace raw_input() with input() and test any number conversions in the script.

Python input() vs raw_input() mainly comes down to Python version and safe handling of user-entered text. For every new script, start with Python 3 input(), validate the result, and convert it explicitly when needed. I hope you found this article helpful.

You May Also Like

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.