Open In App

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

Last Updated : 29 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

C




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


One solution is to use raw_input() two times. 

Python3




x, y = input(),  input()


Another solution is to use split() 

Python3




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 

Python3




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


Python3




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


Instead of using the input function to read a line of input from the user and then processing the line to extract the values, you can use the sys.stdin.readline function to read a line of input and then use the split method and a list comprehension to extract and convert the values to the desired type.

Python3




import sys
 
# Read a line of input
# Split the line into a list of values
# Extract and convert the values to integers using a list comprehension
 
line = [int(x) for x in sys.stdin.readline().split() ]




Previous Article
Next Article

Similar Reads

Assigning multiple variables in one line in Python
A variable is a segment of memory with a unique name used to hold data that will later be processed. Although each programming language has a different mechanism for declaring variables, the name and the data that will be assigned to each variable are always the same. They are capable of storing values of data types. The assignment operator(=) assi
2 min read
How to Catch Multiple Exceptions in One Line in Python?
Prerequisites: Exception handling in python There might be cases when we need to have exceptions in a single line. In this article, we will learn about how we can have multiple exceptions in a single line. We use this method to make code more readable and less complex. Also, it will help us follow the DRY (Don't repeat code) code method Generally t
1 min read
Creating multiple user types and using proxy models in Python Django
In this article, we will understand the concept of the proxy model and the implementation of multiple user types depending on the requirements of your application in Django. What is the Proxy model in Django? An inherited proxy model can have a few extra functionalities, methods, and different behaviors than the parent model as defined by the creat
9 min read
Taking multiple inputs from user in Python
The developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods.  Methods to Take multiple inputs from user Using split() method :Using map() with split():Using List comprehension : Using s
4 min read
Get a list as input from user in Python
We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to get input a list from the user using Python. Example: Input : n = 4, ele = 1 2 3 4Output : [1, 2, 3, 4]Input : n = 6, ele = 3 4 1 7 9 6Output : [3, 4, 1, 7, 9, 6]Get a list as input from user in Python using Loop C/C++ Cod
2 min read
Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then the order of matrix is given by r x c. Each entrie
3 min read
Python program to create dynamically named variables from user input
Given a string input, our task is to write a Python program to create a variable from that input (as a variable name) and assign it to some value. Below are the methods to create dynamically named variables from user input. Using globals() method to create dynamically named variables Here we are using the globals() method for creating a dynamically
2 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Step 1: First, we will take the data from the user and
2 min read
Save user input to a Excel File in Python
In this article, we will learn how to store user input in an excel sheet using Python, What is Excel? Excel is a spreadsheet in a computer application that is designed to add, display, analyze, organize, and manipulate data arranged in rows and columns. It is the most popular application for accounting, analytics, data presentation, etc. Methods us
4 min read
How To Add User Input To A Dictionary In Python
In Python, a dictionary is a built-in data type that represents an unordered collection of key-value pairs. Dictionaries are sometimes referred to as "dicts." They provide a way to store and retrieve data efficiently based on keys. Dictionaries in Python are defined using curly braces {}. In this article, we will add user input to a dictionary in P
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg