How To Get User Input In Python

Getting user input is a crucial part of any programming language. In Python, it is essential to know how to get user input to create interactive programs and scripts. In this tutorial, we will walk you through the steps of getting user input in Python using the input() function. We will also show you how to cast and manipulate the input data as per your requirements.

Step 1: Basic usage of input() function

The simplest way to get user input in Python is by using the built-in input() function. This function reads and returns a line of text as a string from the standard input stream (i.e., the console).

The above script will prompt you to enter your name, and then it’ll print a greeting message with your name.


Enter your name: John Doe
Hello, John Doe!

Step 2: Type casting input data

By default, the input() function returns a string, even when a user enters a number. To store the user input in other types like an integer or a floating-point number, you can use Python’s built-in type conversion functions such as int() and float().

This script prompts the user to enter a number, which gets stored as an integer and a float, respectively.


Enter a number: 42
You entered integer: 42
You entered float: 42.0

Step 3: Handling multiple inputs in a single line

Sometimes, you might want to get multiple input values in a single line, separated by a delimiter such as a space or a comma. In this case, you can use the split() method to break the input string into a list of items.

This script will prompt you to enter two numbers separated by a space, and it will print the sum of those numbers.


Enter two numbers separated by a space: 5 8
The sum of the numbers is: 13

Step 4: Using input() with a list comprehension

You can also use the input() function in combination with a list comprehension to get multiple inputs in a more concise way. Here’s an example of how you can get a list of integer values from the user:

The above script will prompt you to enter a string containing multiple numbers separated by spaces, which it will convert into a list of integers.


Enter a list of numbers separated by space: 1 2 3 4 5
List of integers: [1, 2, 3, 4, 5]

Full code:


In this tutorial, we demonstrated how to get user input in Python using the input() function. You now know how to capture single and multiple inputs, type cast the input data, and manipulate the input as needed. With this knowledge, you can start creating interactive Python programs that accept and process user inputs for various applications.