Home »
Python
Taking multiple inputs from the user using split() method in Python
In this tutorial, we are going to learn how to take multiple inputs from the user using split() method in Python programming language?
Submitted by IncludeHelp, on November 14, 2019
The task is to take multiple inputs from the user, for this we can use split() method.
split() method
split() method is a library method in Python, it accepts multiple inputs and brakes the given input based on the provided separator if we don’t provide any separator any whitespace is considered as the separator.
Syntax:
input().split([separator], [maxsplit])
Here, separator and maxsplit are the optional parameters, they can be used for the specified purposes.
Example without using separator
# input two integer numbers and print them
a, b = input('Enter two integer numbers: ').split()
print("a = ",a, ",b = ",b)
# input name, age and percentages of the student
# and print them
name, age, perc = input("Enter student's details: ").split()
print("Name: ", name)
print("Age: ", age)
print("Percentage: ", perc)
Output
Enter two integer numbers: 100 200
a = 100 ,b = 200
Enter student's details: prem 21 98.56
Name: prem
Age: 21
Percentage: 98.56
Example using separator
# input two integer numbers and print them
a, b = input('Enter two integer numbers: ').split(',')
print("a = ",a, ",b = ",b)
# input name, age and percentages of the student
# and print them
name, age, perc = input("Enter student's details: ").split(',')
print("Name: ", name)
print("Age: ", age)
print("Percentage: ", perc)
Output
Enter two integer numbers: 100,200
a = 100 ,b = 200
Enter student's details: prem,21,98.56
Name: prem
Age: 21
Percentage: 98.56
Python Tutorial
ADVERTISEMENT
ADVERTISEMENT