The 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
- Using List comprehension
Using split() method :
This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, users use a split() method to split a Python string but one can use it in taking multiple inputs.
Syntax :
input().split(separator, maxsplit)
Example :
Python3
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
a, b = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))
x = list(map(int, input("Enter multiple values: ").split()))
print("List of students: ", x)
|
Output:
Enter two values: 5 10
Number of boys: 5
Number of girls: 10
Enter three values: 5 10 15
Total number of students: 5
Number of boys is : 10
Number of girls is : 15
Enter two values: 5 10
First number is 5 and second number is 10
Enter multiple values: 5 10 15 20 25
List of students: [5, 10, 15, 20, 25]
Using List comprehension :
List comprehension is an elegant way to define and create a 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.
Example:
Python3
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
x, y, z = [int(x) for x in input("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))
x = [int(x) for x in input("Enter multiple values: ").split()]
print("Number of list is: ", x)
|
Output :
Enter two values: 5 10
First Number is: 5
Second Number is: 10
Enter three values: 5 10 15
First Number is: 5
Second Number is: 10
Third Number is: 15
Enter two values: 5 10
First number is 5 and second number is 10
Enter multiple values: 5 10 15 20 25
Number of list is: [5, 10, 15, 20, 25]
Note: The above examples take input separated by spaces. In case we wish to take input separated by comma (, ), we can use the following:
Python3
x = [int(x) for x in input("Enter multiple value: ").split(",")]
print("Number of list is: ", x)
|
Please see https://ide.geeksforgeeks.org/BHf0Cxr4mx for a sample run.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
23 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...