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 World

Output:

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 20

Output:

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 5

Output:

['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 40

Output:

[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,40

Output:

[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 15

Output:

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.6

Output:

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 15

Output:

[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 9

Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

โœ… Reads multiple lines of numbers and builds a 2D list.


๐Ÿงพ Summary Table

TaskExample CodeOutput
Read two space-separated valuesa, b = input().split()a, b as strings
Read multiple integerslist(map(int, input().split()))[10, 20, 30]
Read comma-separated valueslist(map(int, input().split(',')))[10, 20, 30]
Read floatslist(map(float, input().split()))[1.2, 3.4]
Read fixed number of valuesx, 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 parts
  • map() โ†’ converts input to desired type
  • list() โ†’ 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 15

Output:

Their sum is: 30

โœ… In summary:
Python makes it easy to handle multiple inputs in one line using a combination of
input() + split() + map() โ€” clean, simple, and powerful.


Scroll to Top