Python User Input

About Python User Input

User input in Python refers to the data or information that a program receives from the user during its execution.

This input can be provided through various means, such as keyboard input, mouse clicks, or other forms of interaction.

The most common way to obtain user input in Python is through the input() function, which allows the program to prompt the user for information and read their response.

How does user input work in Python?
  • The user is prompted to enter information (e.g., name, age, etc.).
  • The program reads the input and stores it in a variable for further processing.
  • The program can then use this input to perform various operations, such as calculations, data processing, or generating output based on the user's input.

We strongly recommend you to practice the examples of this tutorial on VS Code or your preferred Python IDE to get a better understanding of how user input works in Python.

The input() Function

The input() function in Python is used to obtain user input from the console. When called, it displays a prompt (if provided) and waits for the user to enter data. Once the user presses Enter, the function returns the input as a string.

Input() function without prompt(any message)

User input can be taken using the input() function without providing any prompt or message. In this case, the program will simply wait for the user to enter some data and press Enter without displaying any instructions or guidance.

In this example, the program waits for the user to enter his/her name. After the user inputs his/her name and presses Enter, the program greets him/her with a personalized message.

Input() function with prompt

User input can also be taken using the input() function with a prompt or message. This allows the program to provide instructions or guidance to the user about what kind of input is expected.

In this example, the program prompts the user to enter his/her name. After the user inputs his/her name and presses Enter, the program greets him/her with a personalized message.

Convert User Input to Other Data Types

By default, the input() function returns user input as a string. However, you can convert the input to other data types using functions like int(), float(), etc.

In this example, the program prompts the user to enter two numbers. After the user inputs the numbers and presses Enter, the program calculates and displays their sum.

Input validation and error handling

In the following example, if any user enters non-numeric values, the program will crash with a ValueError because it tries to convert the input to an integer without validating it first.

It is necessary to validate user input to ensure that it meets the expected format and type. You can use try-except blocks to handle potential errors that may arise from invalid input.

In this example, the program attempts to convert the user input to integers. If the user enters non-numeric values, a ValueError will be raised, and the program will catch it and display an error message instead of crashing.

Multiple User Inputs

Multiple user inputs can be taken by calling the input() function multiple times. Each call will prompt the user for input and store it in a separate variable. The program can then use these variables to perform various operations or display the collected information.

In the following example, the user is prompted to enter his/her name and age. The program then combines this information to display a personalized message.

Multiple Inputs in One Line

Multiple inputs can be taken in one line by using the split() method to separate the input values based on a delimiter (e.g., space).

In the following example, the user is prompted to enter their first and last name separated by space in one line, and the program splits the input into two separate variables. The program then combines these variables to display the full name.

In the following example, the user is prompted to enter two numbers separated by space. The program splits the input into two parts, converts them to integers, and then calculates and displays their sum.

User Input to Create Lists

You can also take user input to create lists. For example, you can prompt the user to enter multiple values separated by a delimiter and then split the input to create a list.

In this example, the user is prompted to enter color names separated by space. The program splits the input into a list of values and then converts it into a list object.

User Input with Functions

You can also use the input() function within your own functions to gather user input as needed.

In this example, the get_name() function prompts the user to enter his/her name and returns it. The main program then calls this function and prints a welcome message using the inputted name.