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
- Dunzo Off-Campusing Drive | Data Engineer | codewindow.in
- Verbal Ability Questions Solved | Synonyms Antonyms – codewindow.in
- Verbal Ability Questions Solved | Statement Correction – codewindow.in
- Verbal Ability Questions Solved | Spotting Error – codewindow.in
- Verbal Ability Questions Solved | Speech and Tenses – codewindow.in
- Try
- Verbal Ability Questions Solved | Sentence Arrangement – codewindow.in
- Verbal Ability Questions Solved | Prepositions – codewindow.in
- Verbal Ability Questions Solved | Grammar – codewindow.in
- Verbal Ability Questions Solved | Articles – codewindow.in
- Zoho Off Campus Drive | Product Marketing Associate / Specialist | codewindow.in
- BerryWorks Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- Microsoft Off Campus Hiring Drive – Technical Support – Codewindow.in
- Infosys Pseudocede & Puzzle Solved – codewindow.in
- Infosys Verbal Questions Solved – codewindow.in
- Amazon Off Campus Drive | Trainee | codewindow.in
- Hawkins Off Campus Drive | Management trainees | codewindow.in
- Infosys Mathematical Questions Solved – codewindow.in
- Paypal Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- IBM Off Campus Hiring Drive – Associate Systems Engineer – Codewindow.in
- Infosys Off Campus Hiring Drive – SP and DSE – Codewindow.in
- VIRTUSA Recruitment 2022 | Associate Engineer | codewindow.in
- Infosys Logical Questions solved – codewindow.in
- Google Off Campus Hiring Drive – IT Support Engineer – Codewindow.in
- Capgemini Pseudocode Solved | Set 4 – codewindow.in
- Capgemini Pseudocode Solved | Set 3 – codewindow.in
- Capgemini Pseudocode Solved | Set 2 – codewindow.in
- JECA 2022 Question Paper – 90+ Sample Questions Exposed – codewindow.in
- Capgemini Pseudocode Solved | Set 1 – codewindow.in
- Verizon Off Campusing | Software Developer | codewindow.in
- TCS Recruitment Off-Campusing | Service Desk Analyst | codwindow.in
- Amdocs Off-Campusing | Devops Engineer | codewindow.in
Also Checkout
- Algorithm
- Aptitude
- Capgemini Coding Questions
- Capgemini Pseudocode
- CodeVita
- Coding Questions
- Cognizant Placement
- Data Structure and Algorithm
- Epam Full Question Paper
- Guidance for Accenture
- HR Questions
- IBM Questions
- Infosys
- Internship
- Interview Experience
- JECA
- Job Info
- Machine Learning
- Miscellaneous
- NPCI
- Programming in C
- Programming in C++
- Programming in JAVA
- Programming in Python
- Quiz
- Recruiting Companies
- Revature
- Study Material
- TCS NQT
- Tech Mahindra Coding Questions
- Tech Mahindra Questions
- Uncategorized
- Verbal Ability
- Web Development
- Wipro Coding Questions
- Wipro NLTH
- WIpro NLTH Coding Solve