How to take Multiple Input from User in Python
It is a common problem for the beginners. It may ask in the interview. Sometimes, the developers also need to take the multiple inputs in a single line. It can be easily done in the C/C++ using the scanf() method. However, Python provides the two methods that help us to take multiple values or input in one line.
In this tutorial, we will learn how to take multiple inputs in one line using various methods.
Using split() Method
The split() method is useful for getting multiple inputs from users. The syntax is given below.
The split() method is used to split the Python string, but we can use it to get the multiple values.
Let's understand the following example.
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]
In the above code, we take the multiple inputs in a single line. The values are separated by the whitespace, you can use comma (,) or anything.
Using List Comprehension
We can also take values and convert them into the list using the map() method along with the split() method. Let's understand the following example.
Enter multiple values: 4 6 7 2 4 List of values: [4, 6, 7, 2, 4]
We used whitespace as a separator in the above code and taken the input in one line and type casted it into a list.
Take Matrix Input from User
Matrix is a rectangular array, or we can say rectangular arrangement of data or numbers. The matrix can take any value such as integer values, floating values, string, complex numbers, etc. The values are placed horizontally called rows, while vertical values are called 'columns'. If the matrix consists of r number of rows and c number of columns, the order of the matrix will be r x c.
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 above can be implemented in the one line as below.
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() function and numpy Library
There is a popular library called Numpy which can use for any scientific computation. It provides the extensive support for multidimensional arrays. We will use this library for input matrix. Let's understand the following example.
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]]
In this tutorial, we have shown the different ways to take multiple values from the user. It saves the number of code lines and is quite easy to use. We have also described the same as a matrix where we can create a user-define matrix.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week