How To Take Multiple Inputs in Python in one line
Now-a-days several Developers develop multiples apps and websites for users.Developers have no time for doing much coding for developing applications. Therefore, At times, Developers might want to take multiple inputs from the user and in this case, we can use the raw_input ( ) or input( ) function multiple times.
Moreover, These raw_input ( ) or input( ) function take much valuable time of us. In other words, users might not loss their valuable time.
But then, there is an easy way of doing this using some inbuilt functions
However, 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.
- Using
split()
method - BY Using List comprehension
- Using map () function
Take Multiple Inputs from User in Python
a) split ()
split( ) function helps us get multiple inputs from the user and assign them to the respective variables in one line. This function is generally used to separate a given string into several substrings. However, you can also use it for taking multiple inputs. The function generally breaks the given input by the specified separator and in case the separator is not provided then any white space is considered as a separator.
The syntax for using the split function to take multiple inputs is:
input().split(separator, maxsplit)# taking two inputs at a time
x, y =
input("Enter a two value: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
OUTPUTEnter a two value:
12 20Number of boys:
12Number of girls:
20# taking three inputs at a time
x, y, z =
input("Enter a three value: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
OUTPUTEnter a three value:
50 30 20Total number of students:
50Number of boys is :
30Number of girls is :
20
b) List Comprehension
List comprehension is an elegant way to define and create 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.
List data types also helps in taking multiple inputs from the user at a time. The syntax for creating a list and storing the input in it is
x, y=[xforxininput("Enter two value: ").split()]
List allows you to take multiple inputs of different data type at a time. The below example will help you understand better.
# 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 )
OUTPUT
enter two values : 20 50
First number is :20
Second number is :50
# taking multiple inputs at a time
x =[int(x) for x in input("Enter multiple value: ").split()]
print("Number of list is: ", x)
OUTPUT
Enter multiple value: 10 20 50 63 89
Number of list is: [ 10 , 20 , 50 , 63 , 89 ]
Note : The above examples take input separated by spaces. In case we wish to take input separated by comma (, ), we can use following:
# 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)
c) map()
map() is another function that will help us take multiple inputs from the user. In general, the syntax for map function is map (fun, iter). Here fun is the function to which the map function passes each iterable.
Syntax for multiple input using map()
variable 1, variable 2, variable 3 = map(int,input().split())
An example to take integer input from the user.
#multiple inputs in Python using map
x, y = map(int, input("Enter two values: ").split())
print("First Number is: ", x)
print("Second Number is: ", y)
Output:
Enter two values: 7 1
First Number is: 7
Second Number is: 1
In conclusion, This post How To Take Multiple Inputs in Python in one line is important and useful post.
Source : https://www.python.org/