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.
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
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.
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:
Difference Between input and raw_input in Python
Here is a tabular representation of the differences between
raw_input in Python:
|Feature||raw_input (Python 2.x)||input (Python 2.x)||input (Python 3.x)|
|Return Type||Always returns a string||Evaluates the input as Python code||Always returns a string|
|Safety||Safe, as it doesn’t evaluate the input||Unsafe, as it evaluates the input||Safe, as it doesn’t evaluate the input|
|Use case||Reading textual data from the user||Reading Python expressions from user||Reading textual data from the user|
|Availability in Python||Available in Python 2.x||Available in Python 2.x and 3.x||Available 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:
- Fastest sorting algorithm in Python
- get values from json array in Python
- How to Send email using Python?
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.