- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to input multiple values from user in one line in Python?
To read multiple variables in language C, we write something like −
//Read three variable in one line scanf(“%d %d %d”, &x, &y, &z)
Currently python does not have an equivalent to scanf(). However, python provides regular expressions which are more powerful and verbose than scanf() format strings. In Python, to provide multiple values from user, we can use −
input() method: where the user can enter multiple values in one line, like −
>>> x, y, z = input(), input(), input() 40 30 10 >>> x '40' >>> y '30' >>> z '10'
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.
Passing multiple values using list comprehension
>>> x,y,z = [int(x) for x in input().split()] 9 12 15 >>> x 9 >>> y 12 >>> z 15
In above line of code, i have typecast the input values to integers. In case you don’t want that & your inputs are of mixed type, you can simply type −
>>> x,y,z = [x for x in input().split()] 40 10 "hello"
Using map function
Another way to pass multiple values from user is to use map function.
>>> x,y,z = map(int, input().split()) 40 54 90 >>> x 40 >>> y 54 >>> z 90
- How to input multiple values from user in one line in C#?
- How to input multiple values from user in one line in Java?
- Take Matrix input from user in Python
- How to display multiple labels in one line with Python Tkinter?
- Taking multiple inputs from user in Python
- How to catch multiple exceptions in one line (except block) in Python?
- How to concatenate multiple C++ strings on one line?
- Python Get a list as input from user
- Taking input from the user in Tkinter
- Iterate over lines from multiple input streams in Python
- How to populate an array one value at a time by taking input from user in Java?
- How to create input Pop-Ups (Dialog) and get input from user in Java?
- How to execute Python multi-line statements in the one-line at command-line?
- Java Program to Get Input from the User
- How to Create Multiple User Accounts in Linux?