Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python Tutorial - Learn Python Programming Language
Next article icon

While taking a single input from a user is straightforward using the input() function, many real world scenarios require the user to provide multiple pieces of data at once. This article will explore various ways to take multiple inputs from the user inPython.

Using input() and split()

One of the simplest ways to take multiple inputs from a user in Python is by using theinput() function along with thesplit() method. The split() method splits a string into a list based on a specified separator (by default, it uses whitespace).

Example:

Python
# taking two inputs at a timex,y,z=input("Values: ").split()print(x)print(y)print(z)

Output: 

values: 5 6 7
5
6
7

How it Works:

  • input() takes the full input as a single string.
  • .split() divides the string into separate components based on whitespace by default.
  • The values are assigned to individual variables (x, y, z).

Let's take a look at other cases of taking multiple inputs from user in python:

Using List Comprehension (Multiple Inputs in One Line)

If we want to ask the user for multiple values on a single line, we can use list comprehension combined with the input() function and split() to break the input into individual components. Also we can take inputs separated by custom delimiter which is comma in the below example.

Example: 

Python
# Asking for multiple space-separated valuesinputs=[iforiininput().split()]print(inputs)# taking multiple inputs at a time separated by commax=[int(x)forxininput().split(",")]print(x)

Output :

5 6 7 8
['5', '6', '7', '8']

Explanation:

  • The input()function is used to take the user's input as a string.
  • Thesplit() method splits the string based on whitespace (by default) into a list of strings.
  • The list comprehension [item for item in input().split()]takes each element in the split input and stores it in the list inputs.

Note: You can replace comma with any delimiter if you want to take inputs separated by space.

Using map() for Multiple Integer Inputs

If you need to collect multiple inputs in a single line and convert them into integers (or another data type), themap() function is useful. The map() function applies a specified function to each item in an iterable.

Example:

Python
# Take space-separated inputs and convert them to integersa=map(int,input().split())# Convert the map object to a list and print itb=list(a)print(b)

Output:

5 6 7 8 9
[5, 6, 7, 8, 9]

Explanation:

  • We use input() to take a single line of input.
  • The split() method divides the string into a list of substrings.
  • map(int, ...) converts each element in the list to an integer.
  • Finally, we convert the result back into a list to display the values.

Taking Multiple Inputs in a Loop

If you want to collect multiple inputs from the user one at a time, you can use a loop. This is particularly useful when you need to collect an arbitrary number of inputs or perform validation on each input.

Python
# Create an empty list to store the inputsa=[]# Ask the user for how many items they want to inputb=int(input("How many items do you want to enter? "))# Loop to collect multiple inputsforiinrange(b):val=input(f"Enter item{i+1}: ")a.append(val)foriina:print(i)

Explanation:

  • We first ask how many items the user wants to input.
  • We then use a loop to take inputs one by one, appending each input to a list.
  • After the loop finishes, we print the collected items.
  • This approach gives the user the flexibility to enter as many items as needed.

Improve
Practice Tags :

Similar Reads

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 ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp