Python Input | How To Take Multiple Inputs in Python in one line

At times, we might want to take multiple inputs from the user and in this case, we can use the raw_input ( ) or input ( ) function multiple times. But then, there is an easy way of doing this using these inbuilt functions.

a) Using split() function
b) Using input () function
c) Using map () function
d) Using List Comprehension

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

variable 1, variable 2 = input ("Enter what has to be conveyed to the user here"). split()    # for space-separated inputs

variable 1, variable 2 = input (“Enter what has to be conveyed to the user here”). split(” , “)    # for comma seperated inputs

An example is,

# Python program to take multiple inputs from the user
a, b = input("Enter two of your lucky number: ").split() 
print("First lucky number is: ", a) 
print("Second lucky number is: ", b) 
Output:
Enter two of your lucky number: 7 1
First lucky number is: 7
Second lucky number is: 1

b) input ()

You can take multiple inputs in one single line by using the raw_input function several times as shown below.

#multiple inputs in Python using input
x, y = input("Enter First Name: "), input("Enter Last Name: ") 
print("First Name is: ", x) 
print("Second Name is: ", y)
Output:
Enter First Name: FACE
Enter Last Name: Prep
First Name is: FACE
Second Name is: Prep

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

An example to take string input from the user.

#multiple inputs in Python using map
x, y = map(str, input("Enter your first and last name ").split())
print("First Name is: ", x) 
print("Second Name is: ", y)

d) List Comprehension

List date 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 = [x for x in input("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.

#multiple inputs in Python using list comprehension
x, y = [x for x in input("Enter your name and age: ").split(",")]
print("Your name is: ", x) 
print("Your age is: ", y)
Output:
Enter your name and age: FACE Prep, 8
Your name is: FACE Prep
Your age is: 8