Featured image for the blog post "Mastering Multiple Inputs in Python" – a visual introduction to handling user inputs effectively.
When you’re learning Python, one of the first things you’ll do is ask the user for information using the input() function. For example, you might ask them for their name or age. But what if you need to ask for more than one thing at the same time?
In Python, there are simple ways to handle multiple inputs. This is really helpful when your program needs to gather several pieces of information from the user, like their name, age, and favorite color — all in one go.
In this blog post, I’ll show you the easiest ways to get multiple inputs from a user. We’ll go step by step, and by the end, you’ll understand how to:
Don’t worry if it sounds a bit tricky at first! I’ll guide you through everything in a way that’s easy to understand. Let’s get started!
In the real world, many programs need more than one piece of information from the user. For example, think about:
If you want your program to be useful and interactive, handling multiple inputs is a key skill. It helps your program do more complex tasks in an organized way.
When you manage multiple inputs properly, your code becomes shorter and easier to maintain. Instead of writing separate input() calls for each piece of information (which would make your code longer and harder to read), you can collect all the information at once.
When you handle multiple inputs in a well-organized way, your program can guide the user to enter everything in one go. This creates a smoother, more user-friendly experience.
For example, instead of asking for each piece of information one after the other, you can ask the user to enter all their details in one line. This makes your program feel more intuitive.
Handling multiple inputs is a foundational skill that will set you up for more advanced tasks, like:
We’ll use two functions here: input() and split(). These are simple to use but can be really powerful for collecting several pieces of information from the user at once.
input()The input() function waits for the user to type something and then returns that as a string.
For example:
user_input = input("Enter something: ")
print(user_input)
If you run the above code and type “Hello” when prompted, the output would be:
Hello
.split() to Break It DownNow, split() is a method that we can use on the string the user typed to break it into separate pieces.
When you use .split(), Python splits the input at each space by default. So, if you enter several values in one line separated by spaces, split() will split them into separate pieces.
Let’s see this in action.
# Asking the user for their name, age, and country in one line
name, age, country = input("Enter your name, age, and country: ").split()
# Showing the result
print("Name:", name)
print("Age:", age)
print("Country:", country)
input("Enter your name, age, and country: "): Alex 25 USA."Alex 25 USA"..split(): split() method takes that string "Alex 25 USA" and splits it into separate parts: "Alex" (name)"25" (age)"USA" (country)["Alex", "25", "USA"].name, age, country = ...: name, age, and country in order.name = "Alex", age = "25", and country = "USA".print(): name, age, and country to the user.Enter your name, age, and country: Alex 25 USA
Name: Alex
Age: 25
Country: USA
split() function splits a string into a list based on spaces by default. Each element in the list is assigned to a variable on the left side.split() is really useful when you want to take multiple pieces of data in one go.Let’s move on to taking a list of inputs when you don’t know how many items the user will enter. This is useful when you’re working with dynamic data, like asking the user for a list of numbers or a list of names, and you want to handle any number of inputs.
If you don’t know how many items the user will provide, you can still use input() and .split(), but in a slightly different way. Here’s how you can handle it.
If the user needs to input multiple numbers, we can ask them to type all the numbers on one line, separated by spaces. Here’s how you can do it:
# Example 2: Entering a list of numbers
numbers = input("Enter numbers separated by space: ").split()
print("Numbers:", numbers)
input(): This asks the user to enter a list of numbers separated by spaces. 1 2 3 4 5..split(): The split() method splits the string at each space, turning it into a list of strings. ['1', '2', '3', '4', '5'].numbers now holds all the values entered by the user as strings.Since the input() function always returns data as strings, you might want to convert those strings into integers, especially when working with numbers. To do that, we can use map() and int.
Here’s how you do it:
# Convert input strings to integers
numbers = list(map(int, input("Enter numbers: ").split()))
print("Numbers:", numbers)
input(): The user enters the numbers as strings, just like before (e.g., 1 2 3 4 5)..split(): It splits the input into a list of strings: ['1', '2', '3', '4', '5'].map(int, ...): The map() function applies the int function to each string in the list, converting them into integers. map(int, ...), you’ll get a map object that contains integers.list(): The list() function converts that map object into a list of integers.Now, the numbers list contains integers: [1, 2, 3, 4, 5].
This technique is common in coding challenges and automation scripts, where you need to gather a list of inputs quickly and efficiently. It helps you handle dynamic input lengths, meaning the user doesn’t need to specify how many numbers they want to input.
1 2 3 4 5Numbers: [1, 2, 3, 4, 5]
Great! Now that you know how to take multiple inputs using split() and convert them into integers, let me show you a cleaner and more powerful way to do this — using list comprehension.
List comprehension is a short and readable way to create new lists by applying a simple operation to each item in a sequence. When combined with input() and split(), it becomes a really handy tool.
Let’s say you want to ask the user to enter a list of numbers, and you want to print the square of each number. Here’s how you can do it:
# Example 3: Square of each entered number
squares = [int(x)**2 for x in input("Enter numbers: ").split()]
print("Squares:", squares)
input("Enter numbers: ")1 2 3 4..split()['1', '2', '3', '4'].[int(x)**2 for x in ...]x in the list.x to an integer with int(x).**2.print("Squares:", squares)2 4 6Squares: [4, 16, 36]
Perfect! You’re getting the hang of handling multiple inputs. Now let’s talk about a slightly more realistic situation — where the user enters different types of values in a single line.
In many real-life programs, you’ll collect a mix of data types — like a name (which is text), an age (which is an integer), and a salary (which is a float or decimal number).
By default, the input() function gives you strings. So, you need to manually convert each value to the correct type.
# Example 4: Mixed-type inputs
name, age, salary = input("Enter name, age, and salary: ").split()
age = int(age)
salary = float(salary)
print("Name:", name)
print("Age:", age)
print("Salary:", salary)
input(...).split()Alice 30 50000.50['Alice', '30', '50000.50']name gets 'Alice' (string)age gets '30' (but we convert it to int)salary gets '50000.50' (and we convert it to float)Name: Alice
Age: 30
Salary: 50000.5
Always validate user input in real-world apps. For example, make sure the age is a number and not something like "thirty" — or you’ll get a runtime error when trying to convert it with int() or float().
Great! You’ve learned how to handle single-line and mixed-type inputs. Now let’s go one step further—what if the user needs to enter multiple lines of input?
Sometimes, you’ll want your program to accept several lines of input, especially in tasks like batch processing, reading logs, or parsing user-submitted data line by line.
The easiest way to do this is by using a for loop.
# Example 5: Reading 3 lines of input
for i in range(3):
data = input(f"Enter data for line {i+1}: ").split()
print("You entered:", data)
for i in range(3):input(...).split().split() breaks it into individual words or values.print()Enter data for line 1: red blue green
You entered: ['red', 'blue', 'green']
Enter data for line 2: 10 20 30
You entered: ['10', '20', '30']
Enter data for line 3: apple banana
You entered: ['apple', 'banana']
Let’s now take your input handling a step further by learning how to store multiple lines of input — and even better, how to stop input when the user types a specific keyword, like "end".
When you’re not sure how many lines the user will enter, you can collect them in a list and stop based on a condition (for example, typing "end").
# Example 6: Reading input until 'end' is entered
lines = []
while True:
line = input("Enter a line (or type 'end' to stop): ")
if line.lower() == 'end':
break
lines.append(line)
print("You entered:")
for l in lines:
print(l)
while True:input()if line.lower() == 'end': break'end' (in any case: End, END, etc.), the loop stops.lines.append(line)Enter a line (or type 'end' to stop): Hello
Enter a line (or type 'end' to stop): I love Python
Enter a line (or type 'end' to stop): Let's code!
Enter a line (or type 'end' to stop): end
You entered:
Hello
I love Python
Let's code!
You’re now ready to handle structured input — where users enter data in a key-value format, just like you’d see in a form or a configuration file. This is super useful when you’re building interactive scripts or dynamic forms.
sys.stdin for Faster Input (Competitive Programming)When you’re solving problems that involve a lot of input, especially in competitive programming, the regular input() function can be too slow.
That’s where sys.stdin comes in. It helps you read input faster, which is really important when your program needs to handle thousands of inputs quickly.
Here’s how you use it:
import sys
# Example: Reading input from stdin
data = sys.stdin.readline().strip().split()
print("Data:", data)
Let me break it down:
sys.stdin.readline() reads a full line of input..strip() removes any extra spaces or line breaks..split() breaks the line into separate values (usually numbers or words).This method is especially useful when you’re reading a big list of numbers or words in one go.
Sometimes, instead of just plain words or numbers, you want to collect input like this:
name:John age:30 country:India
This gives you structured data in a clean format. With just a bit of Python magic, you can convert it into a dictionary — which lets you access values by keys.
# Example 7: Converting to a dictionary
entries = input("Enter key:value pairs (e.g., name:John age:30): ").split()
data_dict = {k: v for k, v in (item.split(":") for item in entries)}
print("Dictionary:", data_dict)
input(...).split()["name:John", "age:30"]."name:John"), it splits it into key and value.{k: v for ...}Sample Run
Enter key:value pairs (e.g., name:John age:30): name:Alice age:28 city:Paris
Dictionary: {'name': 'Alice', 'age': '28', 'city': 'Paris'}
data_dict["name"].Pro Tip: You can even add code to convert values (like age to an integer) or validate if the keys exist.
Let’s wrap things up with some common mistakes you’ll want to watch out for when working with input() in Python. These issues are easy to run into, especially when you’re just getting started — but once you know them, they’re easy to avoid.
When you use input(), Python always reads the value as a string. If you try to use that string as a number without converting it, you’ll run into trouble.
age = input("Enter your age: ")
print(age + 1) # ❌ This will throw an error!
Fix: Convert it to an integer before using it in a calculation.
age = int(input("Enter your age: "))
print(age + 1)
If you’re unpacking values like this:
name, age = input("Enter your name, age, and country: ").split()
And the user enters three values instead of two, Python will throw a ValueError.
Fix: Make sure the number of inputs matches the number of variables, or collect them into a list.
data = input("Enter your details: ").split()
print(data)
Extra spaces can cause your conditions to fail.
username = input("Enter username: ")
if username == "admin":
print("Access granted")
If the user types admin with a trailing space, it won’t match.
Fix: Use .strip() to remove unwanted spaces.
username = input("Enter username: ").strip()
Learning to spot these early will help you avoid common bugs and make your scripts run more smoothly.
input() Function in PythonLearning how to use the input() function in Python is a foundational skill for writing interactive programs. Whether you’re building a simple form, processing a list of numbers, or handling complex input patterns like key-value pairs, mastering input() helps you create user-friendly and dynamic scripts.
Let’s quickly recap what you’ve learned:
input() in Python and how it works during runtimeEach technique adds to your toolkit — helping you write cleaner, faster, and more interactive code.
input() Function in PythonYou can use the input() function combined with the split() method to take multiple inputs on a single line. For example:
name, age = input(“Enter your name and age: “).split()
This splits the input by spaces and assigns the values to name and age.
You can use split() to get a list of inputs and then convert them into integers using map():
numbers = list(map(int, input(“Enter numbers: “).split()))
This handles any number of inputs, making it useful for dynamic scenarios.
Yes, you can collect input as key-value pairs and convert it into a dictionary using a combination of split() and dictionary comprehension:
entries = input(“Enter key:value pairs: “).split()
data_dict = {k: v for k, v in (item.split(“:”) for item in entries)}
This allows you to process structured input easily.
input() for multiple values?A common mistake is not converting input types. For example, input() returns strings, so if you’re expecting numbers, you need to convert them to int or float:
age = int(input(“Enter your age: “)) # Convert to integer
Always convert the types to avoid errors in calculations.
Python Official Documentation on input()
The official Python documentation provides detailed information on the input() function and other built-in functions. It’s a great place to deepen your understanding.
👉 Python input() Documentation
Python Data Structures: Dictionary and List
This guide explains how to effectively use data structures like lists and dictionaries, which are essential when working with multiple inputs in Python.
👉 Python Data Structures
Stack Overflow Python Community
If you have more advanced or specific questions, the Python community on Stack Overflow is always a good place to get expert answers.
👉 Python Questions on Stack Overflow
After debugging production systems that process millions of records daily and optimizing research pipelines that…
The landscape of Business Intelligence (BI) is undergoing a fundamental transformation, moving beyond its historical…
The convergence of artificial intelligence and robotics marks a turning point in human history. Machines…
The journey from simple perceptrons to systems that generate images and write code took 70…
In 1973, the British government asked physicist James Lighthill to review progress in artificial intelligence…
Expert systems came before neural networks. They worked by storing knowledge from human experts as…
This website uses cookies.