Taking multiple inputs from user in Python
Last Updated :
25 Jul, 2024
The developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods.
Methods to Take multiple inputs from user
Using split() method :
This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, users use a split() method to split a Python string but one can use it in taking multiple inputs.
Syntax :
input().split(separator, maxsplit)
Implementations:
Python
# Python program showing how to
# multiple input using split
# taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
# taking three inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
# taking two inputs at a time
a, b = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))
# taking multiple inputs at a time
# and type casting using list() function
x = list(map(int, input("Enter multiple values: ").split()))
print("List of students: ", x)
Output:
Enter two values: 5 10
Number of boys: 5
Number of girls: 10
Enter three values: 5 10 15
Total number of students: 5
Number of boys is : 10
Number of girls is : 15
Enter two values: 5 10
First number is 5 and second number is 10
Enter multiple values: 5 10 15 20 25
List of students: [5, 10, 15, 20, 25]
Using map() with split():
Using map() with the split() method in Python is a powerful way to efficiently process input values. The split() method allows breaking a string into substrings based on a specified delimiter, often useful for parsing user input. By combining it with map() and a desired transformation function (like int() for converting to integers), we can swiftly convert these substrings into the desired data types and assign them to variables for further processing or calculations.
Syntax:
variable1, variable2, ... = map(datatype, input().split())
Implementations:
Python
#Example with integers
a, b, c = map(int, input("Enter three integers separated by spaces: ").split())
print("Sum:", a + b + c)
#Example with floats:
x, y = map(float, input("Enter two floats separated by a space: ").split())
print("Product:", x * y)
#Example with strings:
name, age = input("Enter your name and age separated by a space: ").split()
print("Hello,", name + "! You are", age, "years old.")
Output:
Enter three integers separated by spaces: 5 10 15
Sum: 30
Enter two floats separated by a space: 3.5 2.5
Product: 8.75
Enter your name and age separated by a space: Alice 25
Hello, Alice! You are 25 years old.
Using List comprehension :
List comprehension is an elegant way to define and create a list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user.
Example:
Python
# Python program showing
# how to take multiple input
# using List comprehension
# taking two input at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
# taking three input at a time
x, y, z = [int(x) for x in input("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
# taking two inputs at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))
# taking multiple inputs at a time
x = [int(x) for x in input("Enter multiple values: ").split()]
print("Number of list is: ", x)
[/GFGTABS]
Output :
Enter two values: 5 10
First Number is: 5
Second Number is: 10
Enter three values: 5 10 15
First Number is: 5
Second Number is: 10
Third Number is: 15
Enter two values: 5 10
First number is 5 and second number is 10
Enter multiple values: 5 10 15 20 25
Number of list is: [5, 10, 15, 20, 25]
Note: The above examples take input separated by spaces. In case we wish to take input separated by comma (, ), we can use the following:
Python
# taking multiple inputs at a time separated by comma
x = [int(x) for x in input("Enter multiple value: ").split(",")]
print("Number of list is: ", x)
Output

Please see https://ide.geeksforgeeks.org/BHf0Cxr4mx for a sample run.
>
Taking multiple inputs from user in Python – FAQs
How to Take Multiple Inputs in Python?
Taking multiple inputs in Python can be done in various ways, depending on how the input is provided and how you want to process it.
How to Use input() Function for Multiple Inputs?
You can use the input() function to take multiple inputs from the user in a single line and then process them accordingly.
How to Split User Input in Python?
You can split the user input using the split() method. This is especially useful when the user inputs multiple values separated by spaces.
Example:
# Taking multiple inputs in a single line
inputs = input("Enter multiple values separated by space: ").split()
# Printing the inputs
print(inputs)
If the user enters: 10 20 30 40, the output will be:
['10', '20', '30', '40']
What is the Use of map() Function with input()?
The map() function is used to apply a specific function to all items in an input list. This is useful for converting all input strings to integers or floats, for example.
Example:
# Taking multiple numeric inputs in a single line and converting them to integers
inputs = list(map(int, input("Enter multiple integers separated by space: ").split()))
# Printing the integer inputs
print(inputs)
If the user enters: 10 20 30 40, the output will be:
[10, 20, 30, 40]
How to Take Multiple Numeric Inputs in Python?
Combining input(), split(), and map() functions is an efficient way to take multiple numeric inputs.
Example:
# Taking multiple numeric inputs in a single line
numbers = list(map(float, input("Enter multiple numbers separated by space: ").split()))
# Printing the numeric inputs
print(numbers)
If the user enters: 1.5 2.5 3.5, the output will be:
[1.5, 2.5, 3.5]
Please Login to comment...