Accepting input from the user is one way to create more dynamic programs and applications. To do so in Python, developers must use the input() function. In this Python tutorial, we will discuss how to use the input() function, learn about its parameters, and how to validate user input data types.
Many programs rely on user input to function. This can include input from a keyboard, database query, or file. The user input could be something as simple as a street address or username, or something a bit more complicated, such as a password or paragraphs of data. If you think about it, an application like Microsoft Word must take in, store, and react to a near-infinite amount of user input.
To that end, user input in a Python application can be used for a single event, to trigger a series of events, be stored for a limited time or permanent basis, and be stored in local files, transmitted over the Internet or a network, or placed in a database.
Whatever its purpose and however it is stored, at the end of the day, in Python, a developer relies on the input() function to accept that user input.
How to Use the input() Function in Python
To accept user input in Python we use the input() function. Below is an example of how to accept user input in Python:
# The easiest way to accept user input in Python name = input() print(name)
The above code is the easiest method of accepting user input from a user. We start by creating a variable, then using the input() function to prompt a user for information. The program will wait until something is typed into the keyboard, then resume once the user presses the Enter key.
Once the user has input some data, our program prints out the value of the name variable.
The problem with this specific example is that, once it is run, the user really has no way of knowing what the program is expecting from them – in essence, it is missing a prompt. To fix this, we have a few options. First, we can use a simple print statement before we ask for input to let the user know what type of information to enter:
print("Enter your first name: ") name = input() print("Nice to meet you", name)
This time around, we print some text to the user’s screen, letting them know that we expect them to enter their first name. The program then pauses and won’t resume again until the user enters some data, and presses the Enter button – the same as before. Finally, we print the user input to the screen. If you run this program and enter a name, such as James, you would get the following result:
Enter your first name: James Nice to meet you James
The input() Function Parameter
A simpler way to achieve the same effect as our previous example is to simply use the input() functions optional parameter or argument. Here is how that looks in Python code:
name = input("Enter your first name") print("Nice to meet you", name)
This version of our code produces the same output as our previous example but requires less coding and is more efficient. It is also less error-prone and offers better readability.
Typecasting and Converting input() to Int
One possible pitfall Python developers might face when accepting user input is the fact that the input() function always returns a string, no matter what a user types in. While this is fine in some cases, in other instances your program might require integer or floating-point values. To avoid this problem, all you need to do is employ typecasting. Typecasting is a method you can use to convert a string (in this instance) to another data type.
To convert user input as an integer, you can use the int() built-in functon. Here is some example code showing how to typecast in Python:
age = int(input('Enter your age: ')) print("You are", age, "years old") print(10 * age)
Run the code above to see how it works. We used two different print statements to show that the text entered after the prompt was, indeed, converted to an int data type. One thing to note: if the user entered in anything other than numeric values, an error would occur because you cannot convert a string into an integer. Because of this, always make sure you account for this possibility in your code.
Converting input() to Floating-point values
Just as you can convert user input to an integer value, so, too, can you convert it to a floating-point number or decimal value. The following code shows you how to convert user input from the input() function to a float in Python:
cost = float(input("Enter the item cost:")) print("You entered the cost of:",cost)
The principle behind this code works the same as when we typecast to an integer, only here we are converting the user input to a float. A few quick things to note here. First, if the user enters anything other than a numeric value, an error will occur. So, once again, make sure you account for this possibility in your code.
The other thing to note is that this program does accept integer values – that is, whole numbers – as input. However, it outputs those whole numbers as floating-point numbers. For example, if you were to enter in the number 1 when prompted in this version of our program, the output would be:
You entered the cost of 1.0
Converting User Input into strings
Even though the input you accept is technically automatically a string, you could – for all intents and purposes – still convert it to a string. To be fair, I cannot think of a reason that you would want to, but just in case you ever need to, just know that it is possible using the keyword str. Here is how that would look in code:
first = str(input("Enter Your First Name: ")) last = str(input("Enter Your Last Name: ")) print("Your Full Name Is: " + first + " " + last)
Running this code and inputting the values James and Payne would result in the output:>
Your Full Name Is: James Payne
Accepting User Input in Older Versions of Python
If you are not using Python 3.x, you will need to revert to using raw_input(). This holds true for Python 2.x versions. Its use is the same as regular input():
name = raw_input("Enter Your Name: ") print(name)