Python input() vs raw_input()

In Python programming, handling user input is a fundamental aspect. In this tutorial, we will explore the differences between the input() and raw_input() functions, and their usage, and provide practical examples to illustrate their functionalities, aiming to enhance your understanding and efficiency in handling user inputs.

input() in Python 3

In Python 3, the input() function is used to capture user input. This function reads a line from input, converts it into a string, and returns it. Here’s a basic example:

# Python 3 example
user_input = input("Enter your name: ")
print(f"Hello, {user_input}!")

Output:

Enter your name: 

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

Python input() vs raw_input()

When you run this code, the program will pause and wait for the user to enter their name. After the user inputs their name and presses Enter, the program will greet the user with their name.

Read Python vs C# [Performance Comparison]

Key Points:

  • The input() function always returns a string.
  • It is used for capturing user input in a more user-friendly and straightforward manner.

raw_input() in Python 2

In Python 2, there are two functions for capturing user input: input() and raw_input(). The raw_input() function reads a line from input, converts it into a string, and returns it, similar to Python 3’s input() function. Here’s an example:

# Python 2 example
user_input = raw_input("Enter your name: ")
print("Hello, {}!".format(user_input))

On the other hand, the input() function in Python 2 evaluates the input as a Python expression. This can lead to unexpected behavior and security vulnerabilities if the input is not properly sanitized. Here’s an example:

# Python 2 example
user_input = input("Enter a number: ")
print("You entered:", user_input)

If the user enters 5, the program will print You entered: 5. However, if the user enters a Python expression like 5 + 5, the program will print You entered: 10.

Check out How to Comment Out Multiple Lines in Python?

Key Points:

  • raw_input() always returns a string.
  • input() evaluates the input as a Python expression, which can be dangerous if not handled correctly.

Examples

Let us consider some examples to distinguish between input() and raw_input()

Example 1: Basic User Input

Python 3:

# Python 3 example
user_input = input("Enter your favorite color: ")
print(f"Your favorite color is {user_input}.")

Python 2:

# Python 2 example
user_input = raw_input("Enter your favorite color: ")
print("Your favorite color is {}.".format(user_input))

Read Is Python an Interpreted Language?

Example 2: Handle Numerical Input

Python 3:

# Python 3 example
age = int(input("Enter your age: "))
print(f"You are {age} years old.")

Python 2:

# Python 2 example
age = int(raw_input("Enter your age: "))
print("You are {} years old.".format(age))

Check out Is Python a Compiled Language?

Example 3: Potential Security Risk in Python 2 input()

Python 2:

# Python 2 example
# This is unsafe and should be avoided
user_input = input("Enter a number: ")
print("You entered:", user_input)

If a user enters a malicious expression, it could be executed, leading to security vulnerabilities.

Check out Is Python a Scripting Language?

Summary

The primary differences between input() in Python 3 and raw_input() in Python 2 are as follows:

Featureinput() in Python 3raw_input() in Python 2input() in Python 2
Return TypeAlways returns a stringAlways returns a stringEvaluates the input as a Python expression
SecuritySafe for user inputSafe for user inputCan be unsafe if input is not sanitized
UsageThe standard for user input in Python 2Standard for user input in Python 2Not recommended due to security concerns
Example Inputinput("Enter your name: ")raw_input("Enter your name: ")input("Enter a number: ")

Read Interfaces in Python

Conclusion

In this tutorial, I have explained the differences between the input() and raw_input() functions with key points. We also differentiated by considering some examples of basic user input, handling numerical input, potential security risk in Python, and summary.

You may also like to read:

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.