Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconMultiple Inputs From User in Python | Python Input Program

Multiple Inputs From User in Python | Python Input Program

Last updated:
29th Jan, 2021
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
Multiple Inputs From User in Python | Python Input Program

Introduction

Programmers often want to create programs where users can enter multiple inputs in Python. They then perform several operations on the input provided by the user. Some inbuilt functions can be used multiple times to take input directly from the user such as raw_input () and input () in data science. Writing the same functions multiple times in a code makes the file heavy and increases code complexity. In this blog, we are going to discuss several methods that collect multiple inputs from the user in one line and reduce code length.

  • Split () function
  • Map () function
  • List comprehension

Collection Multiple Inputs in Python From User

Using Split () Function

With the help of the split () function, developers can easily collect multiple inputs in Python from the user and assign all the inputs to the respective variables. Developers can specify a character that will be used as a separator to break the input provided by the user. If the developer is not providing any separator, then the user input is broken by white space. Usually, this method is used to break a python string into multiple substrings, but it can also be used to collect multiple inputs from the user.

The syntax that is used for the split () method is as follows:

var1, var2 = input (“Type what type of input you want to collect from the user”). split (“/”) for / separated input

var1, var2 = input (“Type what type of input you want to collect from the user”). split () for white space separated input

Example of Split () Function

# Python program for breaking Python string into substring

# taking multiple inputs from the user using the split () function

# collecting three inputs at a time

a, ,b, c = input (“Enter your child age: “).split ()

print(“Enter your elder son’s age: “, a)

print(“Enter your daughter’s age: “, b)

print(“Enter your younger son’s age: “, c)

print()

# collecting four inputs at a time

a, b, c, d = input(“Enter the count of fruits: “).split()

print(“Enter the number of apples you have: “, a)

print(“Enter the number of oranges you have: “, b)

print(“Enter the number of mangoes you have: “, c)

print(“Enter the number of bananas you have: “, d)

print()

# taking two inputs at a time

x, y = input(“Enter your marks: “).split()

print(“Marks obtained in physics is {} and the mark obtained in chemistry is {}”.format(x, y))

print()

Our learners also read: Top Python Courses for Free

Explore our Popular Data Science Courses

The Output for the Above Program is as Follows

Enter your child age: 25 20 14

Enter your elder son’s age: 25

Enter your daughter’s age: 20

Enter your younger son’s age: 14

Enter the count of fruits: 5 15 13 23

Enter the number of apples you have: 5

Enter the number of oranges you have: 15

Enter the number of mangoes you have: 13

Enter the number of bananas you have: 23

Enter your marks: 96 84

Marks obtained in physics is 96 and the mark obtained in chemistry is 84

Using Map () Function

map () is the other function used by the developers to take multiple inputs from the user. The syntax for map () function is as follows:

var1, var, var = map(int,input().split())

Example of Map () Function

#collecting multiple inputs in Python using map () function

a, b = map (int, input(“Enter the count of fruits you have: “).split())

print(“Enter the count of apples you have: “, a)

print(“Enter the count of oranges you have: “, b)

The Output for the Above Program is as Follows

Enter the count of fruits you have: 30 50

Enter the count of apples you have: 30

Enter the count of oranges you have: 50

Also Read: Python Pattern Programs

Read our popular Data Science Articles

Using List () Function

List () is one of the functions used in Python language to define and create a list. List function works similarly as a mathematical statement in one line. It is also used by developers to collect multiple inputs in Python from the user. List () function collects multiple inputs of different data at the same time.

The syntax for creating a list is as follows:

a, b = [afora input(“Type what type of input you want to collect from the user “).split()]

upGrad’s Exclusive Data Science Webinar for you –

Transformation & Opportunities in Analytics & Insights

 

Example for List () Function

# Python program for breaking Python string into substring

# taking multiple inputs from the user using list comprehension

# taking three input at a time

a, b, c = [int(a) for a in input(“Enter the marks obtained: “).split()]

print(“Enter the marks obtained in physics: “, a)

print(“Enter the marks obtained in chemistry: “, b)

print(“Enter the marks obtained in English: “, c)

print()

# taking two input at a time

a, b = [int(a) for a in input(“Enter your child’s age: “).split()]

print(“Enter the age of elder son: “, a)

print(“Enter the age of younger son: “, b)

print()

# taking three inputs at a time

A, b, c = [int(a) for a in input(“Enter the count of fruits: “).split()]

print(“The total number of apples are {} and oranges are {}”.format(a, b))

print()

# taking multiple inputs at a time

a = [int(a) for a in input(“Enter multiple value: “).split()]

print(“The value entered by you are: “, a)

The Output of the Above Program is as Follows

Enter the marks obtained: 94 90 96

Enter the marks obtained in physics: 94

Enter the marks obtained in chemistry: 90

Enter the marks obtained in English: 96

Enter your child’s age: 34 23

Enter the age of elder son: 34

Enter the age of younger son: 23

Enter the count of fruits: 76 23

The total number of apples are 76 and oranges are 23

Enter multiple value: 23 45 76 48 90 76

The value entered by you are: [23, 45, 76, 48, 90, 76]

Also Read: Python Project Ideas & Topics

Check out all trending Python tutorial concepts in 2024

Conclusion

The code given in this blog is for example purpose and can be modified as per an individual’s needs.

If you are curious to learn about data science, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

More curious? Learn Python Input and Output Tutorials for free.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Frequently Asked Questions (FAQs)

1Why is list comprehension considered superior to loop?

List comprehension is a superior way of taking multiple inputs in Python, even better than the conventional loop process in many ways:
1. List comprehension is much faster and compact than the loop since it collects all the elements first and inserts them all together at once.
2. The same thing that a loop does in a block can be done in a single line using a list comprehension, making the code cleaner and more user-friendly.
3. Resolving a matrix into a vector and list filtration are some of the best examples where the list comprehensions can be seen outperforming a loop.

2How can I take multiple inputs in my program?

Python provides multiple ways of taking input. There are three significant ways of taking input that is mentioned below:
1. Using split(): The split() function is widely used to take multiple inputs In Python from the user. It accepts a separator as its parameter which determines which character will be used to separate the string. The syntax of the split() function is:
string.split(separator, maxsplit)
2. Using map(): The map() is another way of taking inputs from the user. The syntax of the map() function in Python is:
map(func, ite)
3. Using List comprehensions: This method is the most optimized of all three methods. It is the most convenient and elegant way of creating a list in Python. However, we can also take multiple inputs in Python from the user through this method.

3Name some of the most used built-in methods provided by Python.

Python has gained a lot of popularity among programmers and developers, and most of the credit goes to the built-in functions and methods it provides. These functions save a lot of time and reduce code length, thus making Python the first choice for projects.
The following are some of the most popular and useful Python built-in functions and methods:
1. bin(): This function takes an integer as an argument and returns its binary string.
2. dict(): This function accepts a collection of items and creates a dictionary with key-value pairs.
3. abs(): This function accepts a number as an argument and returns its absolute value.
4. ascii():This function takes a string as an argument and returns it in a readable format by replacing the non-ASCII characters with x, u or U escapes.

Explore Free Courses

Suggested Blogs

Python Developer Salary in India in 2024 [For Freshers & Experienced]
908659
Wondering what is the range of Python developer salary in India? Before going deep into that, do you know why Python is so popular now? Python has be
Read More

by Sriram

21 May 2024

Binary Tree in Data Structure: Properties, Types, Representation & Benefits
89018
Data structures serve as the backbone of efficient data organization and management within computer systems. They play a pivotal role in computer algo
Read More

by Rohit Sharma

21 May 2024

Data Analyst Salary in India in 2024 [For Freshers & Experienced]
22123
Summary: In this Article, you will learn about Data Analyst Salary in India in 2024. Data Science Job roles Average Salary per Annum Data Scient
Read More

by Shaheen Dubash

20 May 2024

Python Free Online Course with Certification [2024]
134601
Summary: In this Article, you will learn about python free online course with certification. Programming with Python: Introduction for Beginners Le
Read More

by Rohit Sharma

20 May 2024

13 Interesting Data Structure Projects Ideas and Topics For Beginners [2023]
248169
 In the world of computer science, understanding data structures is essential, especially for beginners. These structures serve as the foundation for
Read More

by Rohit Sharma

20 May 2024

Top 30 Python Pattern Programs You Must Know About
40847
Summary Pattern in Python or “Python patterns” is an essential part of Python programming, especially when you are just starting out with using algor
Read More

by Rohit Sharma

19 May 2024

15 Exciting Data Science Project Ideas &  Topics for Beginners [2024]
956149
Summary: In this Article, you will learn about 15 exciting data science project ideas & topics for beginners. 1. Beginner Level | Data Science P
Read More

by Rohit Sharma

16 May 2024

Binary Tree vs Binary Search Tree: Difference Between Binary Tree and Binary Search Tree
63063
Introduction Sorting is the process of arranging the data in a systematic order so that it can be analysed more effectively. The process of identifyi
Read More

by Rohit Sharma

16 May 2024

Top 12 Fascinating Python Applications in Real-World [2024]
157589
It is a well-established fact that Python is one of the most popular programming languages in both the coding and Data Science communities. But have y
Read More

by Rohit Sharma

16 May 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon