The Wayback Machine - https://web.archive.org/web/20240905202145/https://www.geeksforgeeks.org/input-multiple-values-user-one-line-python/
Open In App

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

Last Updated : 29 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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?
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 to handle exceptions we use try/Except metho
2 min read
How to fix Python Multiple Inheritance generates "TypeError: got multiple values for keyword argument".
Multiple inheritance in Python allows a class to inherit from more than one parent class. This feature provides flexibility but can sometimes lead to complications, such as the error: "TypeError: got multiple values for keyword argument". This error occurs when the method resolution order (MRO) leads to ambiguous function calls, especially with key
5 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
6 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 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
Compare two Files line by line in Python
In Python, there are many methods available to this comparison. In this Article, We'll find out how to Compare two different files line by line. Python supports many modules to do so and here we will discuss approaches using its various modules. This article uses two sample files for implementation. Files in use: file.txt file1.txt Method 1: Using
3 min read
Read a file line by line in Python
Prerequisites: Open a file Access modes Close a file Python provides inbuilt functions for creating, writing, and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s, and 1s). In this article, we are going to study reading line by line from a file. Learning Ef
7 min read
Break a long line into multiple lines in Python
Break a long line into multiple lines, in Python, is very important sometime for enhancing the readability of the code. Writing a really long line in a single line makes code appear less clean and there are chances one may confuse it to be complex. Example: Breaking a long line of Python code into multiple lines Long Line: a = 1 + 2 + 3 + 4 - 5 * 2
4 min read
PyQtGraph - Setting Symbol of Line in Line Graph
In this article we will see how we can set symbols of line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A li
3 min read
PyQtGraph - Setting Shadow Pen of Line in Line Graph
In this article, we will see how we can set shadow pen of the line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, et
3 min read
PyQtGraph - Setting Pen of Line in Line Graph
In this article we will see how we can set pen of the line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A li
4 min read
PyQtGraph - Setting Alpha Value of Line in Line Graph
In this article we will see how we can set alpha value of the line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, et
4 min read
PyQtGraph - Clearing the Line in Line Graph
In this article, we will see how we can clear the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A line c
3 min read
PyQtGraph - Getting Data Bounds of the Line in Line Graph
In this article we will see how we can get data bounds of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, et
4 min read
PyQtGraph - Getting Data of Line in Line Graph
In this article we will see how we can get data of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A l
3 min read
PyQtGraph - Getting Name of Line in Line Graph
In this article we will see how we can get name of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A l
4 min read
PyQtGraph - Setting Symbol Pen of Line in Line Graph
In this article we will see how we can set symbol pen of line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) A
4 min read
PyQtGraph - Setting Symbol Size of Line in Line Graph
In this article we will see how we can set symbol size of line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.)
4 min read
PyQtGraph - Getting Pixel Padding of Line in Line Graph
In this article we will see how we can get pixel padding of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video,
4 min read
PyQtGraph - Setting Symbol Brush of Line in Line Graph
In this article we will see how we can set symbols brush of line in line graph of the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.
3 min read
PyQtGraph - Getting Starting Co-ordinates of Line in Line Graph
In this article, we will see how we get the starting coordinates of the line i.e origin of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for
4 min read
PyQtGraph - Setting X and Y Co-ordinates of Line in Line Graph
In this article, we will see how we can set X & Y coordinates of the line i.e origin of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for
3 min read
PyQtGraph - Getting X and Y Co-ordinates of Line in Line Graph
In this article, we will see how we can get X & Y coordinates of the line i.e origin of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for
4 min read
PyQtGraph - Setting Starting Co-ordinates of Line in Line Graph
In this article, we will see how we set the starting coordinates of the line i.e origin of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for
3 min read
PyQtGraph - Getting Cursor of Line in Line Graph
In this article, we will see how we can get a custom cursor of the line, of the line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots,
4 min read
PyQtGraph - Setting Cursor for Line in Line Graph
In this article we will see how we can set a custom cursor to the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video
4 min read
PyQtGraph - Getting Increased Inclination Angle of Line in Line Graph
In this article we will see how we can get the increased inclination angle of the line of line graph in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying da
4 min read
Article Tags :
Practice Tags :