Logo

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

In Python 2, there were two functions for getting user input:

  1. raw_input(): Returned user input as a plain string.
  2. input(): Attempted to evaluate the user input as Python code (e.g., typing 1+2 would return 3).

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

  1. Python 2:

    • raw_input() → returns a string.
    • input() → evaluates the input as code (not recommended for untrusted input).
  2. Python 3:

    • raw_input()No longer exists.
    • input()Always returns a string (just like raw_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’s input() does exactly what raw_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