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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.