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.

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:
| Feature | input() in Python 3 | raw_input() in Python 2 | input() in Python 2 |
|---|---|---|---|
| Return Type | Always returns a string | Always returns a string | Evaluates the input as a Python expression |
| Security | Safe for user input | Safe for user input | Can be unsafe if input is not sanitized |
| Usage | The standard for user input in Python 2 | Standard for user input in Python 2 | Not recommended due to security concerns |
| Example Input | input("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:
- Is Python a High Level Language?
- JavaScript vs Python for Web Development: Choosing the Right for Your Project
- Is Python an Object-Oriented Language?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.