raw input function in Python

In this Python tutorial, we will discuss the raw input function in Python, and also the difference between raw_input and input in Python. raw_input function is used in Python 2.x.

Python 2.x

raw_input Function

In Python 2.x, raw_input is a built-in function used to read data from the user as a string. This means that whatever you enter as input is treated as a string.



prompt is an optional parameter that you can use to display a message before the input.


# Python 2.x
user_input = raw_input("Enter something: ")
print("You entered:", user_input)

In this example, Python will display the message “Enter something: “, and whatever you type will be stored as a string in the user_input variable.

input Function

In Python 2.x, the input function reads the input and evaluates it as a Python expression. This essentially means that the input is parsed and evaluated as if it were a Python code.




# Python 2.x
user_input = input("Enter a number: ")
print("You entered:", user_input, "and its square is:", user_input**2)

In this example, if you enter 3, Python 2.x would interpret it as an integer, and you would get the square of 3 in the output.

Python 3.x

In Python 3.x, raw_input has been removed, and the input function behaves like raw_input from Python 2.x. This means that the input function in Python 3.x reads the input as a string.


# Python 3.x
user_input = input("Enter something: ")
print("You entered:", user_input)

This example in Python 3.x behaves the same way as the raw_input example in Python 2.x.

You can see the output below:

raw_input python
raw_input python

Difference Between input and raw_input in Python

Here is a tabular representation of the differences between input and raw_input in Python:

Featureraw_input (Python 2.x)input (Python 2.x)input (Python 3.x)
Return TypeAlways returns a stringEvaluates the input as Python codeAlways returns a string
SafetySafe, as it doesn’t evaluate the inputUnsafe, as it evaluates the inputSafe, as it doesn’t evaluate the input
Use caseReading textual data from the userReading Python expressions from userReading textual data from the user
Availability in PythonAvailable in Python 2.xAvailable in Python 2.x and 3.xAvailable in Python 3.x (behaves like raw_input in Python 2.x)


When working with Python 2.x, use raw_input to safely read strings, and be cautious with input as it evaluates input as code. In Python 3.x, use input to read strings as raw_input is no longer available. I have also shown, the difference between input and raw_input in Python.

You may also like: