Take Multiple Inputs From The User In A Single Line Of Python Code

I am a self-taught Python developer who loves to write on Python Programming and quite obsessed with Machine Learning.
Search for a command to run...

I am a self-taught Python developer who loves to write on Python Programming and quite obsessed with Machine Learning.
No comments yet. Be the first to comment.
When you’re building an application, one thing you can’t skip is API testing. Whether it’s a login flow, payment gateway, or a complex e-commerce workflow, ensuring your APIs behave correctly saves you from nasty surprises in production. I had alread...

PyPy is an implementation of Python written in RPython (Reduced Python) language, and it is seen as a replacement for CPython. PyPy claims that it is almost a drop-in replacement for CPython and can beat it on speed and memory usage. It supports libr...

You must have used functions provided by the os module in Python several times in your projects. These could be used to create a file, walk down a directory, get info on the current directory, perform path operations, and more. In this article, we’ll...

FastAPI is a fast and modern web framework known for its support for asynchronous REST API and ease of use. In this article, we’ll see how to stream videos on frontend in FastAPI. StreamingResponse Stream Local Video FastAPI provides a StreamingRespo...

Have you ever come across circular imports in Python? Well, it’s a very common code smell that indicates something’s wrong with the design or structure. Circular Import Example How does circular import occur? This import error usually occurs when two...
In Python, the input() function allows taking input from the user, and to provide a message with the input() function, we can prompt a string with it.
If we use a simple approach like a beginner then we would write a Python program like below to take multiple inputs.
x = input("Enter the input here: ")
y = input("Enter the input here: ")
z = input("Enter the input here: ")
There are some ways through which we can restrict the above code to a single line for taking multiple inputs from the user in Python. One-liners in Python are always powerful which helps us to write complex code in one line which in turn increases productivity and saves time.
We are going to see two methods through which we can achieve our goal
split() method
list comprehension
Generally, the split() method is used to split a Python string into a list but we can use it for taking multiple inputs from the user.
It will split the specified values in the input() function using a specified separator and if there is no specified separator then any whitespace is a separator.
Here we’ll use split() method with input() function.
input().split(separator, maxsplit)
separator: The string will separate at the specified separator. If it’s not provided then any whitespace is a separator.
maxsplit: A string will split into a maximum of a specified number of times. The default is -1, which means there is no limit.
x, y, z = input("Enter 3 inputs: ").split()
print(f"\n1st input: {x}")
print(f"2nd input: {y}")
print(f"3rd input: {z}")
Output
Enter 3 inputs: Hello there geeks
1st input: Hello
2nd input: there
3rd input: geeks
We haven’t defined any separator and maxsplit parameters. Here we can use only three inputs because we’ve defined only three variables.
But there is a way to take unlimited inputs.
x = input("Enter your inputs: ").split()
print(f"\nInputs: {x}")
Output
Enter your inputs: Hello there geeks I am Sachin
Inputs: ['Hello', 'there', 'geeks', 'I', 'am', 'Sachin']
# Using a comma as a separator
x, y, z = input("Enter the inputs with commas: ").split(",")
print(f"\n1st input: {x}")
print(f"2nd input: {y}")
print(f"3rd input: {z}")
# Using maxsplit
x = input("Enter your input: ").split(",", maxsplit=1)
print(f"\nMaxsplit 1: {x}")
Output
Enter the inputs with commas: hey, there, geeks
1st input: hey
2nd input: there
3rd input: geeks
Enter your input: hey, there, geeks
Maxsplit 1: ['hey', 'there, geeks']
In the first block of code, we specified the separator (,) and the inputs were separated by the commas hence we got the three separate outputs.
In the second block of code, we specified a separator and maxsplit=1 and we can see the output is split into two parts instead of getting three separate outputs.
It is the same process as we did in the first half of this article. Basically, we are going to use the split() but in the list comprehension method.
x = [x for x in input("Enter your inputs: ").split()]
print("Inputs: ", x)
# Taking integers as an input
y = [int(y) for y in input("Enter the values: ").split()]
print("List of students: ", y)
Output
Enter your inputs: There are 4 students in a class of 30 students
Inputs: ['There', 'are', '4', 'students', 'in', 'a', 'class', 'of', '30', 'students']
Enter the values: 23 43 30 45
List of students: [23, 43, 30, 45]
# Using separator
y = [int(y) for y in input("Enter the values: ").split("-")]
print("List of students: ", y)
# Using letter as a separator
y = [y for y in input("Enter the values: ").split("s")]
print("List of animals: ", y)
Output
Enter the values: 30-45-67-34-23-41
List of students: [30, 45, 67, 34, 23, 41]
Enter the values: DogsCatsRacoonsSpider
List of animals: ['Dog', 'Cat', 'Racoon', 'Spider']
We can use any type of separator whether it can be punctuation or letters.
One-liners can be very useful and helpful to enhance code quality as well as increase productivity. Well, we learned how to take multiple inputs from users in a single line of Python code instead of writing multiple lines of code.
We used the split() method which is primarily used for splitting the Python string into a list and then we used the list comprehension technique which offers a shorter syntax to define and create a list in Python.
That’s all for now
Keep Coding✌✌