Taking Multiple Inputs From User in Python
Taking Multiple Inputs From User in Python
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 a multiple inputs from user. It breaks the given input by the specified separator.
If a separator is not provided then any white space is a separator. Generally, user use a split() method to split
a Python string but one can use it in taking multiple input.
Syntax :
input().split(separator, maxsplit)
Example :
Python3
filter_none
edit
play_arrow
brightness_4
# Python program showing how to
print()
print()
# taking two inputs at a time
print()
Output:
Using List comprehension :
List comprehension is an elegant way to define and create 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
filter_none
edit
play_arrow
brightness_4
# Python program showing
print()
print()
print()
Output :
Note : The above examples take input separated by spaces. In case we wish to take input separated by
comma (, ), we can use following:
Python3
filter_none
edit
play_arrow
brightness_4
# taking multiple inputs at a time separated by comma
Recommended Posts:
Python dictionary with keys having multiple inputs
Python - Distance between collections of inputs
Find the average of an unknown number of inputs in Python
How to input multiple values from user in one line in Python?
Taking input in Python
Python VLC MediaPlayer – Taking Screenshot
Taking Screenshots using pyscreenshot in Python
Taking input from console in Python
User-defined Exceptions in Python with Examples
Fetch top 10 starred repositories of user on GitHub | Python
Python | User groups with Custom permissions in Django
Python | Get a list as input from user
Take Matrix input from user in Python
Python | Fetch your gmail emails from a particular user
Python User defined functions
Add a User in Linux using Python Script
Deleting a User in Linux using Python Script
Send message to Telegram user using Python
Python Tweepy - Getting the screen name of a user
Python - User object in Tweepy
ABHISHEK TIWARI 13
Check out this Author's contributed articles.
If you like GeeksforGeeks and would like to contribute, you can also write an article
using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article
appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rakeshsshankala, eramitkrgupta
Article Tags :
Python
python-input-output
thumb_up
65
To-do Done
2.1
Based on 28 vote(s)
Improve Article
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Post navigation
Previous
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
emailfeedback@geeksforgeeks.org
Company
About Us
Careers
Privacy Policy
Contact Us
Learn
Algorithms
Data Structures
Languages
CS Subjects
Video Tutorials
Practice
Courses
Company-wise
Topic-wise
How to begin?
Contribute
Write an Article
Write Interview Experience
Internships
Videos
@geeksforgeeks , Some rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you
have read and understood our Cookie Policy & Privacy PolicyGot It !