"Taking Multiple Inputs from User in Python"
"Taking multiple inputs from user in Python"
[ "Taking Multiple Inputs from User in PythonIn Python, we can take multiple inputs 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 from the user using the split() methodWe 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 from the user using split() methodIn this example, we will take two inputs from the user and then print them.num1, num2 = input("Enter two numbers: ").split()print("First number is: ", num1)print("Second number is: ", num2)Output: Enter two numbers: 2 3The first number is: 2The second number is: 3Method 2: Taking multiple inputs from the user using the list() functionWe 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() functionIn 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 4List of students: ['1', '2', '3', '4']Method 3: Taking multiple inputs from the user using the map() functionWe can use this method to take multiple inputs 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 from user using map() functionIn 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 3First number is: 2Second number is: 3Method 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 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 5First number is: 4Second number is: 5" ]