How to Take Multiple Inputs in One Line in Python
In Python, you can take multiple inputs in one line by using
input().split() which splits the input string by spaces into a list. Then, you can convert each input to the desired type using map() or list comprehension.Syntax
Use input().split() to read multiple values separated by spaces in one line. Then convert them to the needed type.
input(): reads a line of text from the user.split(): splits the string into parts by spaces (default).map(type, iterable): applies a type conversion to each input.
python
values = input().split() # values is a list of strings # To convert to integers: numbers = list(map(int, values))
Example
This example reads three numbers from one line, converts them to integers, and prints their sum.
python
a, b, c = map(int, input().split()) print(a + b + c)
Output
10
Common Pitfalls
Common mistakes include:
- Not converting input strings to numbers before calculations.
- Entering fewer or more inputs than expected causes errors.
- Using
input()withoutsplit()reads only one string.
Always ensure the number of inputs matches the variables or list size.
python
a, b = input().split() # expects exactly two inputs # Wrong: trying to add strings without conversion # a, b = input().split() # print(a + b) # This concatenates strings, not adds numbers # Right: a, b = map(int, input().split()) print(a + b)
Quick Reference
Summary tips for taking multiple inputs in one line:
- Use
input().split()to separate inputs. - Use
map()to convert input types. - Unpack inputs directly into variables if count is known.
- Use list comprehension for flexible conversions.
Key Takeaways
Use input().split() to read multiple inputs separated by spaces in one line.
Convert inputs to the correct type using map() or list comprehension before using them.
Make sure the number of inputs matches the number of variables when unpacking.
Without split(), input() reads the whole line as one string, not multiple values.