There are various methods of taking multiple inputs in a single line.
Method 1:
One of the method is split() method. This methods splits the input separated by separator.
Syntax:
input().split(separator)
Example 1:
# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# for taking two inputs
# using split() method
x, y = input().split()
print("First: ", x)
print("Second: ", y)
Input:
4 5
Output:
First: 4
Second: 5
Explanation:
Here the two inputs are split into two integers and assigned to two variables (x, y).
Example 2:
# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# for taking three inputs
# using split() method
x, y, z = input().split()
print("First: ", x)
print("Second: ", y)
print("Third: ", z)
Input:
4 5 8
Output:
First: 4
Second: 5
Third: 8
Explanation:
Here the three inputs are split into three integers and assigned to three variables (x, y, z).
Example 3:
# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using split() method
x = input().split()
print(x)
Input:
4 5 8
Output:
['4', '5', '8']
Explanation:
Here the three inputs are split into three integers and assigned to a single variable (x) as strings inside a list.
Note: Here the individual inputs are a string type. We cannot type cast it as Integer here.
Method 2:
Using the map() function. map() returns an object so it needs to be converted into a list or tuple. This method maps the input to multiple integer or single integer according to the requirements.
Syntax:
variable = list(map(data_type, input().split(separator)))
Example 1:
# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using map() function
x = list(map(int, input().split()))
print(x)
Input:
4 5 8
Output:
[4, 5, 8]
Explanation:
Here it mapped all the integers to a single variable (x) as integers only inside a list.
Example 2:
# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using map() function
# It can also take input as a string
x = list(map(str, input().split()))
print(x)
Input:
code hustle repeat
Output:
['code', 'hustle', 'repeat']
Explanation:
Here all the characters separated by as space are returned to a single list as strings.
Example 3:
# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using map() function
x = list(map(int, str(int(input()))))
print(x)
Input:
456789
Output:
[4, 5, 6, 7, 8, 9]
Explanation:
Here it returned all single digit variables without any separator to a list as six different integers.
Method 3:
Using list comprehension.
Example 1:
# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using list comprehension
x = [int(x) for x in input()]
print(x)
Input:
456
Output:
[4, 5, 6]
Explanation:
Here the loop returns all single digit variables without any separator to a single list as three different integers.
Example 2:
# Python program to understand how to take multiple inputs in one line/single line
# www.codewindow.in
# using list comprehension
# for assigning it to multiple variables
x, y, z = [int(x) for x in input()]
print("First: ",x)
print("Second: ",y)
print("Third: ",z)
Input:
456
Output:
First: 4
Second: 5
Third: 6
Explanation:
Here the loop returns all single digit variables without any separator to three different variables as three different integers.
You Missed
- Software Engineering – codewindow.in
- Software Engineering – codewindow.in
- Software Engineering – codewindow.in
- Software Engineering – codewindow.in
- Software Engineering – codewindow.in
- Software Engineering – codewindow.in
- MongoDB – codewindow.in
- MongoDB – codewindow.in
- MongoDB – codewindow.in
- Software Engineering – codewindow.in
- Software Engineering – codewindow.in
- Software Engineering – codewindow.in
- Software Engineering – codewindow.in
- PHP & MySQL – codewindow.in
- PHP & MySQL – codewindow.in
- PHP & MySQL – codewindow.in
- Certificates
- PHP & MySQL – codewindow.in
- PHP & MySQL – codewindow.in
- PHP & MySQL – codewindow.in
- CSS – codewindow.in
- CSS – codewindow.in
- CSS – codewindow.in
- Database Management System – codewindow.in
- Database Management System – codewindow.in
- JQuery – codewindow.in
- JQuery – codewindow.in
- MongoDB – codewindow.in
- MongoDB – codewindow.in
- MongoDB – codewindow.in
- Database Management System – codewindow.in
- MongoDB – codewindow.in
Also Checkout
- Adobe
- Ajax
- Algorithm
- Amagi
- Amazon Interview Questions
- Angular JS
- Aptitude
- big data
- Books
- Bridge2i
- C programming
- Campgemini Interview Questions
- Capgemini Coding Questions
- Capgemini Pseudocode
- Celebal Tech
- Cloud Computing
- code nation
- CodeVita
- Coding Questions
- Cognizant Placement
- commvault Systems
- Computer Network
- CSS
- CTS
- Data Science
- Data Structure and Algorithm
- Database Management System
- De Show Interview Questions
- deloitte
- Deutsche Bank Interview questions
- Epam Full Question Paper
- Exxon Mobil interview questions
- filpkart
- Fractal Analytics Interview Questions
- Genpact
- Grab
- GreyB Interview Questions
- Guidance for Accenture
- Gupshup
- HCL Interview Questions
- Hexaware
- HFCL
- HR Questions
- HTML5
- IBM Interview questions
- IBM Questions
- Incture Interview Questions
- Infineon Technologies Interview Questions
- Infosys
- Infosys Interview Questions
- Internship
- Interview Experience
- Interview Questions
- ITC Infotech
- itron
- JavaScript
- JECA
- Job Info
- JQuery
- Kantar Interview Questions
- Larsen & Turbo
- Latenview AnalyticsInterview questions
- Lexmark International Interview Questions
- Machine Learning
- Media.net
- Mindtree Interview Questions
- Miscellaneous
- MongoDB
- Morgan Stanly Interview Questions
- nagarro
- navi
- NodeJS
- NPCI
- NTT Data Interview Questions
- NVDIA
- NVDIA interview questions
- Optum
- PayU
- Persistent INterview Questions
- PHP and MYSQL
- Programming in C
- Programming in C++
- Programming in JAVA
- Programming in Python
- Project
- pseudocode
- PWC Interview Questions
- Python
- Quiz
- Razorpay
- ReactJS
- Recruiting Companies
- Revature
- salesforce
- Samsung
- Schlumberger
- Seimens
- Slice
- Smart Cube
- Software Engineering
- Study Material
- Tally Solutions
- tata cliq
- TCS
- TCS NQT
- TCS NQT Coding Questions
- Tech Mahindra Coding Questions
- Tech Mahindra Questions
- Teg Analytics
- Tejas Network Interview Questions
- Texas Instrument Interview Questions
- Tiger Analytics
- Uncategorized
- UnDosTres
- Unstop
- Verbal Ability
- Web Development
- Wipro Coding Questions
- Wipro interview Questions
- Wipro NLTH
- WIpro NLTH Coding Solve
- Zenser
- Zoho Interview Questions