blog image

In Python, we can take "multiple inputs in Python" from the user using the following methods:

- Using split() method - Using list() function - Using map() function - Using raw_input() function (in Python 2.x) - Using input() function (in Python 3.x)

Let's see each of these methods one by one.

Method 1: Taking multiple inputs in Python from the user using the split() method

We can use this method to take multiple inputs from the user. This is generally used with a string input taken from the user. The string is then converted into a list by using the split() method. Then, we can convert this list into a tuple or dictionary as per our requirement.

Example 1: Taking multiple inputs in Python from the user using split() method

In this example, we will take two inputs from the user and then print them.

num1, num2 = input("Enter two numbers: ").split()

Output: Enter two numbers: 2 3

The first number is: 2 The second number is: 3

Method 2: Taking multiple inputs from the user using the list() function

We can use this method to take multiple inputs from the user. This is generally used when we want input for a list. The input() method takes a string input and then converts it into a list.

Example 2: Taking multiple inputs from user using list() function

In this example, we will take two inputs from the user and then print them.
num = list(map(int, input("Enter a multiple value: ").split()))
print("List of students: ", num)

Output: Enter a multiple value: 1 2 3 4
List of students: ['1', '2', '3', '4']


Method 3: Taking multiple inputs from the user using the map() function

We can use this method to take multiple inputs in Python from the user. This is generally used when we want input for a list. The map() function in Python takes two arguments:

- r = map(func, seq)

Here, r is the resultant list, func is the function that is applied to all the elements of seq and seq is the sequence or list.

Example 3: Taking multiple inputs in Python from user using map() function

In this example, we will take two inputs from the user and then print them.
x, y = map(int, input("Enter a two value: ").split())

print("First number is: ", x)
print("Second number is: ", y)

Output: Enter a two value: 2 3

First number is: 2
Second number is: 3

Method 4: Taking multiple inputs from user using raw_input() function (in Python 2.x)

We can use this method to take multiple inputs from the user. This is generally used with a string input taken from the user. The string is then converted into a list by using the split() method. Then, we can convert this list into a tuple or dictionary as per our requirement.

Example 4: Taking multiple inputs in Python from user using raw_input() function (in Python 2.x)

In this example, we will take two inputs from the user and then print them.
num1, num2 = raw_input("Enter two numbers: ").split()
print("First number is: ", num1)
print("Second number is: ", num2)
Output: Enter two numbers: 4 5
First number is: 4
Second number is: 5