raw_input() functions are used to collect user input.
input() has replaced
raw_input() in Python 3 and onward. Both functions return a user input as a string.
Processing user input is a crucial part of programming. For example, you may want to ask a user their age so you can determine whether they should be allowed to use your site. Or you may want to ask a user to input their name so you can determine its length. Whatever data you need from a user, you’ll need to find a way to get it in some fashion.
That’s where the Python
input() function comes in.
Input(), a built-in function in Python, allows coders to receive information through the keyboard, which they can process in a Python program. In this tutorial, we are going to break down the basics of Python
input() and discuss how you can use the function in your code.
Python Input Receiving
Python uses two functions to receive user input:
raw_input(). The function you’ll use will depend on the version of Python you use, as we discuss below.
When we call our
input() function, our program will stop until the user enters in the text through the Python shell or command line. For example, we may have an
input() function that asks a user for their email address. When this function executes, our program will prompt the user to enter their email and wait until they press
enter. So, the user received an input that stems from the keyboard.
The user must press the
enter key; then the program will continue running.
Python 2.x raw_input()
raw_input() function in Python 2.x can be used to collect user input. The
raw_input() function will prompt a user to enter text into the program, and will collect that data when the user presses the
raw_input() takes one parameter: the message a user receives when they are prompted for input. Here is an example of the Python
raw_input() function in action which uses the input string
Enter your email address:
email = raw_input("Enter your email address: ") print "To confirm, is your email address:", email
When we run our code and give it our email address, our prompt will be printed like this:
Enter your email address: firstname.lastname@example.org To confirm, is your email address: email@example.com
Python 3 input()
raw_input() function was removed in Python 3 and replaced with the
input() function. Although the name of the function has changed, the way in which it works is exactly the same. Here is an example of the Python 3
input() function in action:
email = input("Enter your email address: ") print("To confirm, is your email address:", email)
Here is the result of our code:
Enter your email address: firstname.lastname@example.org To confirm, is your email address: email@example.com
By default, the
raw_input() functions will convert the data a user enters into a string. If you want to work with a string, this is not a problem. But if you’re going to work with an integer or another data type, you’ll need to change it manually.
To do this, you should use the built-in Python data functions to change the type of data you have. Here’s an example of a program that will prompt a user for their age, add one year onto their age, and returns the value:
user_age = input("Enter your age: ") final_user_age = int(user_age) + 1 print("On your next Birthday, you'll be:", final_user_age)
As you can see, we convert the user’s age to an integer using the
int(user_age) function, then we add
1 to that age. On the final line of our code, we allow our value to print on the screen, alongside a short message. Here’s our code in action:
Enter your age: 19 On your next Birthday, you'll be: 20
Alternatively, we could wrap our
input() statement with the
int() function, which will convert our value to a number immediately. Here’s an example:
user_age = int(input("Enter your age: "))
Now that we have our data, we could perform any string formatting functions we want, and manipulate the data based on our needs.
raw_input() functions have a wide array of uses in Python, and if you’re looking to get data from a user, you’ll likely need to use these functions at some point.
In this tutorial, we have broken down how the
raw_input() functions work in Python, and how you can use them to collect user input. Now you’re an expert on collecting user input in Python!