How to Accept Multiple User Inputs in PythonThat is a typical issue among novices. It may inquire during the interview. Sometimes, developers must also accept numerous inputs on a single line. That is straightforward in C/C++ using the scanf() technique. Python has two techniques that enable us to accept numerous values or input on a single line.
- Using split() method
- Using List Comprehension
In this article, we will learn how to accept numerous inputs in a single line using different techniques.Method split() UseThe split() technique is helpful for collecting various user inputs. The syntax is provided in the following section.Syntax –
- input().split(separator, maxsplit)
Parameters –
- The separator parameter breaks the input by the specified separator. By default, whitespace is the specified separator.
The split() function is often used to divide a Python string, but it may also be used to get several values.Let’s analyse the following illustration.Example –
- # taking two inputs at a time
- a, b, c = input(“Enter three values: “).split()
- print(“Enter Your First Name: “, a)
- print(“Enter Your Last Name: “, b)
- print(“Enter Your Class: “, c)
- print()
- # taking three inputs at a time
- x, y, z = input(“Enter three values: “).split()
- print(“Total number of students: “, x)
- print(“Number of passed student : “, y)
- print(“Number of failed student : “, z)
- print()
- # taking four inputs at a time
- a, b, c, d = input(“Enter four values: “).split()
- print(“First number is {}, second number is {} third is {} and fourth is {}”.format(a, b, c, d))
- print()
# taking three inputs at a time
x, y, z = input(“Enter three values: “).split()
print(“Total number of students: “, x)
print(“Number of passed student : “, y)
print(“Number of failed student : “, z)
print()
# taking four inputs at a time
a, b, c, d = input(“Enter four values: “).split()
print(“First number is {}, second number is {} third is {} and fourth is {}”.format(a, b, c, d))
print()
Output:
Enter three values: David Warner MCA Enter Your First Name: David Enter Your Last Name: Warner Enter Your Class: Warner Enter three values: 100 67 33 Total number of students: 100 Number of passed students : 67 Number of failed students : 33 Enter four values: 1 2 3 4 First number is 1, second number is 2 third is 3 and fourth is 4 Enter multiple values: 4 6 7 2 4 List of students: [4, 6, 7, 2, 4]
Explanation: In the above code, several inputs are collected on a single line. The values are separated by whitespace; commas (,) or anything else may be used.Using List UnderstandingUsing the map() and split() methods, we can also turn values into a list from a set of values. Let’s analyse the following illustration.Example -2:
- # Taking multiple inputs in a single line
- # and type casting using list() function
- x = list(map(int, input(“Enter multiple values: “).split()))
- print(“List of students: “, x)
Output:
Enter multiple values: 4 6 7 2 4 List of values: [4, 6, 7, 2, 4]
Explanation: In the preceding code, we utilised whitespace as a separator and typecast the input on one line as a list.Take Matrix User Matrix input is a rectangle array, or rectangular collection of data or numbers. The matrix may accommodate any value, including integers, floating-point numbers, strings, complex numbers, etc. The horizontal values are referred to as “rows,” while the vertical values are referred to as “columns.” If the matrix has r rows and c columns, the order of the matrix will be r x c.Example –
- row = int(input(“Enter the number of rows:”))
- column = int(input(“Enter the number of columns:”))
- # Initialize matrix
- matrix = []
- print(“Enter the entries row-wise:”)
- # For user input
- for i in range(row): # A for loop for row entries
- a =[]
- for j in range(column): # A for loop for column entries
- a.append(int(input()))
- matrix.append(a)
- # For printing the matrix
- for i in range(row):
- for j in range(column):
- print(matrix[i][j], end = ” “)
- print()
# Initialize matrix
matrix = []
print(“Enter the entries row-wise:”)
# For user input
for i in range(row): # A for loop for row entries
a =[]
for j in range(column): # A for loop for column entries
a.append(int(input()))
matrix.append(a)
# For printing the matrix
for i in range(row):
for j in range(column):
print(matrix[i][j], end = ” “)
print()
Output:
Enter the number of rows:3 Enter the number of columns:3 Enter the entries row-wise: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
The preceding may be accomplished in a single line as shown below:Example –
- row = int(input(“Enter the number of rows:”))
- column = int(input(“Enter the number of columns:”))
- matrix = [[int(input()) for x in range (column)] for y in range(row)]
- print(matrix)
Output:
Enter the number of rows:3 Enter the number of columns:3 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
Using map() and numpy LibraryNumpy is a popular package that may be used for any scientific calculation. It offers significant support for arrays with many dimensions. This library will be used for the input matrix. Let’s analyse the following illustration.Example –
- import numpy as np
- row = int(input(“Enter the number of rows:”))
- column = int(input(“Enter the number of columns:”))
- print(“Enter the entries in a single line (separated by space): “)
- # User input of entries in a
- # single line separated by space
- entries = list(map(int, input().split()))
- # For printing the matrix
- matrix = np.array(entries).reshape(row, column)
- print(matrix)
row = int(input(“Enter the number of rows:”))
column = int(input(“Enter the number of columns:”))
print(“Enter the entries in a single line (separated by space): “)
# User input of entries in a
# single line separated by space
entries = list(map(int, input().split()))
# For printing the matrix
matrix = np.array(entries).reshape(row, column)
print(matrix)
Output:
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]
ConclusionThis article demonstrates the various methods for collecting numerous values from the user. It reduces the amount of lines of code and is fairly simple to use. We have also expressed the same as a matrix where a user-defined matrix may be created.