. Advertisement .
..3..
. Advertisement .
..4..
Reading data from the user is a common task of many programs, which can be done easily with raw_input() in Python 2. But what happens if you switch to the new version?
Let’s find out what options you have when you want to use raw_input() in Python 3.
input() – Replacement Of raw_input() In Python 3
The raw_input() function in Python 2 allows you to take input from a user and store it in a variable. This is a simple example of using this function.
name = raw_input("Enter your name: ")
nationality = raw_input("Enter your nationality: ")
print("Your name is {}, and your nationality is {}".format(name, nationality))
Output:
Enter your name: John
Enter your nationality: American
Your name is John, and your nationality is American
The snippet above asks for two pieces of personal information about the user and prints them to the interactive console.
Python 3.x, however, has removed this function. When you try to use raw_input() in this version, you will run into a NameError exception.
name = raw_input("Enter your name: ")
nationality = raw_input("Enter your nationality: ")
print("Your name is", name, ", and your nationality is", nationality)
Output:
Traceback (most recent call last):
File "code.py", line 1, in <module>
name = raw_input("Enter your name: ")
NameError: name 'raw_input' is not defined
To read input from the user in Python 3, you must use the input() function. Its syntax is:
input([prompt])
It has only one argument, which is optional. When you call this function, the whole execution of your Python program is halted until you provide input. The input() function reads this line from you and converts it into a string.
Example:
text = input()
print("You entered:", text)
Output:
How to use raw_input() in Python 3
You entered: How to use raw_input() in Python 3
When an argument is given, the program will display a prompt before taking your input. You can take multiple inputs and then print them with print().
Example:
name = input("Enter your name: ")
nationality = input("Enter your nationality: ")
print("Your name is", name, ", and your nationality is", nationality)
Output:
Enter your name: Patrick
Enter your nationality: French
Your name is Patrick , and your nationality is French
Keep in mind that input() always returns a string value, no matter what kind of input it has read.
year_of_birth = input("Enter your year of birth: ")
print("Data type of year_of_birth is:", type(year_of_birth))
Output:
Enter your birth year: 1988
Data type of year_of_birth is: <class 'str'>
The birthyear variable has the string type, even though you have given the input() function an integer.
Other Uses Of input() In Python 3
If you want to get an integer value from the user, you can use functions like int() to make the conversation.
Example:
from datetime import date
year_of_birth = int(input("Enter your year of birth: "))
current_year = date.today().year
age = current_year - year_of_birth
print("You are", age, "years old.")
Output:
Enter your year of birth: 1987
You are 35 years old.
In the example above, the input() still returns a string value. But it is then converted to an int value by int() and assigned to the birthyear variable.
You can also directly evaluate an expression given to input() by the user.
Example:
expression = input("Enter your expression: ")
result = eval(expression)
print("The value of your expression:", result)
Output:
Enter your expression: (2*8+5*5)+5
The value of your expression: 46
Conclusion
There is no raw_input() in Python 3. But you can use the input() function, which basically does the same job. It can also work with other functions to accomplish more complicated tasks with inputs from the user.
Leave a comment