The input() function in Python makes it very easy to get input from users of your application. The input function is built in just like the print function and its use is quite common. When you call the input function(), you can think of it as an expression that evaluates to whatever the user typed in at the prompt. This always gets returned as a string so if you want to use numbers as input, you will need to handle that by using the int() function. we’ll see several examples of how to use this function here.
The input() Function
- Takes in a prompt
- Displays prompt on the screen
- The user can type a response to the prompt
- Returns user input as a string
User Name As Input
One of the classic examples of the input function in Python is getting the name of the user of your application. So let’s go ahead and do that now we can write some code that will prompt the user for their name and store that result in a ‘name’ variable. Here we go:
The snippet above is actually something called a Jupyter Notebook, which is a really interesting tool to use for Python programming. By using Jupyter Notebook in this example, it makes it really easy to see what the prompt looks like when calling that input() function. We can type some characters into the prompt for our name. Then, we can add the code to print() that variable.
We might want to make that output just a little more interesting. In the following Jupyter Cell, we can concatenate some additional text with the result stored in the name variable to make the output a little more pleasing.
Using input() In A Loop
You might find a case where you want to prompt the user for some input more than once. In that case, you can use the input function inside of a loop. The loop will continue to prompt the user for some input until a specific condition is met. In this next example, we are going to ask the user what are some names that they like. The logic for this is put inside of a while loop. Inside the while loop, we can use the if-else construct to determine if we are going to print out the name or break out of the loop.
Using input() With Numbers
In order for your program to be useful, you might need to accept input in the form of numbers from your user. Let’s look at a simple example that uses the input function to get 2 numbers from the user. The goal of this program is to record those two values, add them together, and print out the result for the user. Let’s try this now.
Wait a second. What happened here? Our program asked the user for the first number. We typed in the number 5. The program then asked the user for the second number. We typed in the value of four. Then the program prints out a message that the sum of five and four is 54. That doesn’t seem correct, does it? Let’s try this again with a small adjustment.
int() and str()
Before we look at the code that is going to fix the bug that the above example has, let’s talk about two new utility functions in Python. These are the int() function and the str() function. These functions allow you to transform a data type between an integer and a string. Y’all need to do this because it is quite possible to add numbers together in Python, and it is also possible to add strings together in Python. What you cannot do is to add integers and strings to each other. So this next example is a refactor of the program making use of both the int() and str() functions so that we get the output we would expect.
input() with Loop, int(), and str()
In this example, we will make use of the input function inside of a while loop in Python. The program will ask the user for a number or they can type the letter Q to quit the program. While the condition is true inside of the loop the program will convert the number that the user typed from decimal to binary format. For each number the user types, the program will print out the binary equivalent.
Show Me A Dog!
We have one more program to go over that makes perfect use of the input() function. This program is going to ask a user if they would like to see a dog. I mean, who doesn’t want to see a Dog, right?! So what we can do is have the user simply type the letter ‘y’ if they want to see a dog, and ‘n’ if they do not. We’ll put this inside of a loop so that you can keep looking at dogs for as long as you like. We needed to add some special modules from IPython.display such as display and HTML in order for images to display inside of our Jupyter notebook. Let’s see this in action.
Python input() Function Summary
Python input() is a built-in function used to read a string from the user’s keyboard. The input() function returns the string typed in by the user in the standard input. When you call this input() function, the Python Interpreter waits for user input from the keyboard. When the user enters types in some data, each character is echoed to the output. This helps the user as feedback of what value is being inputted. After you enter the value and hit Return or Enter key, the string that is typed in by you until this last enter key is returned by the input() function.