Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to input multiple values from user in one line in Python?
In Python, to get multiple values from users in a single line, there are several approaches. The most common methods use split() with map() or list comprehension to parse space-separated input.
Using Multiple input() Calls
The basic approach uses separate input() calls for each value ?
x, y, z = input(), input(), input() print(x) print(y) print(z)
If the user enters 5, 10, 15 in separate prompts, the output will be ?
5 10 15
This method requires multiple user inputs, which is not ideal for single-line input.
Using map() with split()
The map() function applies a conversion function to each element from split() ?
# For space-separated input like "10 20 30"
a, b, c = map(int, "10 20 30".split())
print("Values:", a, b, c)
print("Sum:", a + b + c)
Values: 10 20 30 Sum: 60
Using List Comprehension
List comprehension provides a more readable approach for converting input values ?
# For space-separated input like "5 15 25"
numbers = [int(x) for x in "5 15 25".split()]
a, b, c = numbers
print("First:", a)
print("Second:", b)
print("Third:", c)
print("Product:", a * b * c)
First: 5 Second: 15 Third: 25 Product: 1875
Handling Different Data Types
You can mix different data types by applying specific conversions ?
# For mixed input like "John 25 85.5"
data = "John 25 85.5".split()
name = data[0]
age = int(data[1])
score = float(data[2])
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Score: {score}")
Name: John Age: 25 Score: 85.5
Comparison
| Method | Best For | Readability |
|---|---|---|
Multiple input()
|
Separate prompts | Low |
map() |
Same data type | High |
| List comprehension | Complex conversions | Medium |
Conclusion
Use map(int, input().split()) for same-type numeric input. Use list comprehension for complex conversions or when you need the intermediate list. Both methods are more efficient than multiple input() calls.
