Taking multiple inputs from user in Python
Last Updated: 30-09-2020
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
# multiple input using split
# taking two inputs at a time
x, y = input("Enter a two value: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
print()
# taking three inputs at a time
x, y, z = input("Enter a three value: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
print()
# taking two inputs at a time
a, b = input("Enter a two value: ").split()
print("First number is {} and second number is {}".format(a, b))
print()
# taking multiple inputs at a time
# and type casting using list() function
x = list(map(int, input("Enter a multiple value: ").split()))
print("List of students: ", x)
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
# how to take multiple input
# using List comprehension
# taking two input at a time
x, y = [int(x) for x in input("Enter two value: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print()
# taking three input at a time
x, y, z = [int(x) for x in input("Enter three value: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
print()
# taking two inputs at a time
x, y = [int(x) for x in input("Enter two value: ").split()]
print("First number is {} and second number is {}".format(x, y))
print()
# taking multiple inputs at a time
x = [int(x) for x in input("Enter multiple value: ").split()]
print("Number of list is: ", x)
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
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn
the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python
DS Course.
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 [email protected]. 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 [email protected] to report any issue with the above content.
Post navigation
Previous
first_page Python | Calendar Module
Next
last_pagePython | Pandas Index.asof()
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
Most popular in Python
Python map() function
Adding new column to existing DataFrame in Pandas
Iterate over a list in Python
Python program to convert a list to string
Enumerate() in Python
More related articles in Python
Read a file line by line in Python
How to get column names in Pandas dataframe
Python String | replace()
Reading and Writing to text files in Python
append() and extend() in Python
room5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305
[email protected]
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 !