In Python, you can read multiple values from a single line of input using the input() function combined with string splitting and type conversion.
This is a common interview question and very useful for handling space-separated or comma-separated user input.
Letโs go step by step ๐
๐น 1. Basic Input Reminder
The input() function reads input as a string from the user.
data = input("Enter something: ")
print(data)Example Input:
Hello WorldOutput:
Hello Worldโ Everything entered by the user is treated as a single string.
๐น 2. Reading Multiple Values in One Line (Using split())
The split() method separates a string into parts based on spaces (or another delimiter).
๐งฉ Example:
a, b = input("Enter two values: ").split()
print("Value of a:", a)
print("Value of b:", b)Input:
10 20Output:
Value of a: 10
Value of b: 20โ
split() divides the input string by spaces and assigns the parts to variables.
๐น 3. Reading More Than Two Values
If you donโt know how many inputs will be given, you can read all values into a list.
values = input("Enter multiple numbers: ").split()
print(values)Input:
1 2 3 4 5Output:
['1', '2', '3', '4', '5']โ All elements are stored as strings in a list.
๐น 4. Converting Input to Integers (Using map())
If you want numeric input (like integers or floats), you must convert the strings.
numbers = list(map(int, input("Enter numbers: ").split()))
print(numbers)Input:
10 20 30 40Output:
[10, 20, 30, 40]โ
map(int, ...) converts each string to an integer.
โ
list() stores them in a list.
๐น 5. Reading Comma-Separated Values
You can also split by a custom delimiter, e.g., a comma (,).
numbers = list(map(int, input("Enter numbers separated by commas: ").split(',')))
print(numbers)Input:
10,20,30,40Output:
[10, 20, 30, 40]โ
split(',') splits the string at commas instead of spaces.
๐น 6. Reading and Unpacking Dynamic Inputs
You can unpack input values directly into multiple variables if you know the count.
x, y, z = map(int, input("Enter three numbers: ").split())
print(x, y, z)Input:
5 10 15Output:
5 10 15โ Each space-separated number is mapped to a variable.
๐น 7. Reading Float Values
You can use float instead of int when reading decimal numbers.
a, b, c = map(float, input("Enter three float values: ").split())
print(a, b, c)Input:
1.2 3.4 5.6Output:
1.2 3.4 5.6โ Works exactly like integers but allows decimal input.
๐น 8. Using List Comprehension (Alternative to map())
You can also use list comprehension to achieve the same result.
numbers = [int(x) for x in input("Enter numbers: ").split()]
print(numbers)Input:
5 10 15Output:
[5, 10, 15]โ
Equivalent to using map(int, ...), but more readable for some developers.
๐น 9. Example: Reading a Matrix (2D Input)
For multiple lines of multiple values:
rows = 3
matrix = [list(map(int, input(f"Enter row {i+1}: ").split())) for i in range(rows)]
print(matrix)Input:
1 2 3
4 5 6
7 8 9Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]โ Reads multiple lines of numbers and builds a 2D list.
๐งพ Summary Table
| Task | Example Code | Output |
|---|---|---|
| Read two space-separated values | a, b = input().split() | a, b as strings |
| Read multiple integers | list(map(int, input().split())) | [10, 20, 30] |
| Read comma-separated values | list(map(int, input().split(','))) | [10, 20, 30] |
| Read floats | list(map(float, input().split())) | [1.2, 3.4] |
| Read fixed number of values | x, y, z = map(int, input().split()) | x, y, z as integers |
| Read multiple lines (2D list) | [list(map(int, input().split())) for _ in range(n)] | [[...], [...]] |
โ In short:
You can read multiple values from a single input in Python using:
a, b = input().split() numbers = list(map(int, input().split()))
split()โ splits input into partsmap()โ converts input to desired typelist()โ stores all values
๐งฉ Final Example
# Read 3 integers in one line
x, y, z = map(int, input("Enter 3 numbers: ").split())
print(f"Their sum is: {x + y + z}")Input:
5 10 15Output:
Their sum is: 30โ
In summary:
Python makes it easy to handle multiple inputs in one line using a combination ofinput() + split() + map() โ clean, simple, and powerful.
