Skip to content
Related Articles

Related Articles

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

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 19 Nov, 2020
Improve Article
Save Article

For instance, in C we can do something like this:




// Reads two values in one line
scanf("%d %d", &x, &y) 

One solution is to use raw_input() two times.




x, y = input(),  input()

Another solution is to use split()




x, y = input().split()

Note that we don’t have to explicitly specify split(‘ ‘) because split() uses any whitespace characters as a delimiter as default.

One thing to note in the above Python code is, both x and y would be of string. We can convert them to int using another line

x, y = [int(x), int(y)]

# We can also use  list comprehension
x, y = [int(x) for x in [x, y]]

Below is complete one line code to read two integer variables from standard input using split and list comprehension




# Reads two numbers from input and typecasts them to int using 
# list comprehension
x, y = [int(x) for x in input().split()]  




# Reads two numbers from input and typecasts them to int using 
# map function
x, y = map(int, input().split())

This article is contributed by Abhishek Shukla. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!