How to input multiple values from user in one line in Python?



In Python, to get values from users, use the input(). This works the same as scanf() in C language.

Input multiple values from a user in a single line using input()

To input multiple values in one line, use the input() method −

x, y, z = input(), input(), input()

Let’s say the user entered 5, 10, 15. Now, you can display them one by one −

print(x)
print(y)
print(z)

Output

5
10
15

From above output, you can see, we are able to give values to three variables in one line. To avoid using multiple input() methods(depends on how many values we are passing), we can use the list comprehension or map() function.

Input multiple values from a user in a single line using list comprehension

We have used the List Comprehension with the input() method to input multiple values in a single line −

a,b,c = [int(a) for a in input().split()]

Let’s say the user entered 1, 2, 13 Now, you can display them one by one −

print(x)
print(y)
print(z)

Output

1
2
3

Input multiple values from a user in a single line line using map()

The map() method can also be used in Python to input multiple values from a user in a single line −

a,b,c = map(int, input().split())

Let’s say the user entered 1, 2, 13. Now, you can display them one by one −

print(x)
print(y)
print(z)

Output

1
2
3

Advertisements