Developers usually expect users to enter multiple values ??or inputs in a row. In C++/C, users can use scanf to get multiple inputs in one line, but in Python, users can get multiple values ??or inputs in one line in two ways.
Using split() method : This feature helps to get multiple inputs from the user. It breaks the given input by the specified delimiter. If no separator is provided, any spaces are separators. Normally, users use the split() method to split Python strings, but you can use it to get multiple inputs.
Syntax :
input().split(separator, maxsplit)
Example :
# Python program showing how to # multiple input using split # taking two inputs at a time x, y = input("Enter a two value: ").split() print("Number of boys: ", x) print("Number of girls: ", y) print() # 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) print() # taking two inputs at a time a, b = input("Enter a two value: ").split() print("First number is {} and second number is {}".format(a, b)) print() # taking multiple inputs at a time # and type casting using list() function x = list(map(int, input("Enter a multiple value: ").split())) print("List of students: ", x)
Using List comprehension :
# Python program showing # how to take multiple input # using List comprehension # taking two input at a time x, y = [int(x) for x in input("Enter two value: ").split()] print("First Number is: ", x) print("Second Number is: ", y) print() # taking three input at a time x, y, z = [int(x) for x in input("Enter three value: ").split()] print("First Number is: ", x) print("Second Number is: ", y) print("Third Number is: ", z) print() # taking two inputs at a time x, y = [int(x) for x in input("Enter two value: ").split()] print("First number is {} and second number is {}".format(x, y)) print() # taking multiple inputs at a time x = [int(x) for x in input("Enter multiple value: ").split()] print("Number of list is: ", x)
Note: The above examples take input separated by spaces. In case we wish to take input separated by comma (, ), we can use the 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)
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.