Explore programming tutorials, exercises, quizzes, and solutions!
Python Basic Input and Output Exercises
1/20
You're learning how Python handles user input and output. One of the first things beginners do is read input from the user and display results using the built-in input() and print() functions.
Consider the following code:
name = input("Enter your name: ")
print("Hello,", name)
What does this example teach about Python’s input/output system?
In Python:
input() reads user input as a string, regardless of what the user types.
print() is used to display output to the console.
These functions are built-in, so you can use them without importing any module.
For example:
age = input("Enter your age: ")
print("You entered:", age)
Even if you type a number, input() will treat it as a string unless you explicitly convert it (e.g., using int() or float()).
This makes Python’s input/output system simple, readable, and beginner-friendly.
What will be the output of the following code if the user enters 10?
x = input("Enter a number: ")
print(type(x))
The input() function always returns a string, even if the user types a number. So the type of x is str.
Which function is used to display output to the screen in Python?
The print() function outputs text or variables to the console in Python.
What will the following code print?
print("Hello", "World")
The print() function adds a space between arguments by default. So "Hello", "World" becomes "Hello World".
Which keyword ends the output of a print() statement with a custom string instead of a newline?
The print() function has an end parameter that defines what to print at the end instead of the default newline \n.
What will be the output of the following code if the user enters 5 and 10?
a = input("Enter first number: ")
b = input("Enter second number: ")
print(a + b)
The input() function always returns values as strings, even when the user enters numbers. So when you concatenate the variables a and b, Python treats them as strings and not as numbers. Therefore, a + b results in string concatenation, giving the output "5" + "10" = "510".
If you want to perform arithmetic operations with these values, you need to explicitly convert them to integers or floats using int() or float() functions, respectively.
What will this code output?
print("Python", "is", "awesome", sep="-")
The sep parameter in the print() function is used to specify the string that will be inserted between each of the items to be printed. By default, print() separates items with a space. In this case, we set sep="-", so the items will be separated by a hyphen.
So the output will be "Python-is-awesome".
What will the following code output?
a = input("Enter a number: ")
print(int(a) * 2)
This code will raise a TypeError if the user enters a non-numeric value. The input() function returns a string, and when we attempt to convert it to an integer using int(a), if the string cannot be converted to a number (for example, if the user enters something like "abc"), Python will raise a ValueError.
To ensure this works, the user must enter a valid number (e.g., 5). For safety, you might want to use a try-except block to handle invalid inputs gracefully.
What will be the output if the user enters "12" and "8" when asked for two numbers?
a, b = input("Enter two numbers: ").split()
print(int(a) + int(b))
The split() method splits the input string at spaces by default, so when the user enters "12 8", the string is split into two parts, 12 and 8, which are stored in a and b, respectively. Then, int(a) and int(b) convert them into integers, and the + operator adds them together. The result is 12 + 8 = 20.
What will be the output of this code if the user enters Python?
name = input("Enter your name: ")
print("Hello, " + name)
The input() function stores the value entered by the user (in this case, "Python") in the variable name. The print() function concatenates the string "Hello, " with the value of name, resulting in the output "Hello, Python".
What will be the output of the following code if the user inputs 5?
num = input("Enter a number: ")
print(num * 3)
Since input() returns a string, multiplying the string "5" by 3 will repeat it three times, producing "555". This is string replication, not arithmetic multiplication.
If you want to multiply numerically, you must convert it to an integer using int().
Which of the following statements correctly takes two inputs from the user on the same line and stores them in two variables?
The split() method divides a single input string into parts based on whitespace. When combined with multiple assignment, like x, y = ..., it allows collecting multiple inputs from one line.
Example: if the user enters "10 20", x gets "10" and y gets "20".
What is the purpose of the end parameter in the print() function?
The end parameter in print() specifies what should be printed at the end of the statement. By default, it is a newline character, so each print() call appears on a new line.
For example, using end=" " will print the next output on the same line with a space in between.
What will be printed by the code below if the user inputs 3 4 5?
a, b, c = input("Enter three numbers: ").split()
print(c, b, a)
The input is split into three strings: a = "3", b = "4", c = "5". The print() function outputs them in reverse order: c, b, a → "5 4 3".
This shows how you can manipulate input values immediately after splitting them into variables.
What will this code display if the user inputs apple banana?
first, second = input("Enter two words: ").split()
print("Second word is:", second)
The split() method separates the input into two parts. The first word is assigned to the variable first, and the second word to second.
So when printing second, the output is "banana".
What will be the output if the user enters 10 and abc in the following code?
x, y = input("Enter two values: ").split()
print(int(x) + int(y))
This code tries to convert both input values to integers using int(). While "10" can be converted successfully, "abc" cannot. Python will raise a ValueError at runtime when trying to convert "abc" to an integer.
Which of the following input lines would cause an error in this code?
x, y = input("Enter two numbers: ").split()
The split() function splits the input string by spaces. In this case, the code expects exactly two values to be provided. If only one value is entered, like "10", it cannot unpack the result into two variables x and y, which leads to a ValueError due to mismatched number of values.
What will the following code output if the user types "3 4"?
a, b = map(int, input("Enter two numbers: ").split())
print(a * b)
The input string "3 4" is split into ["3", "4"], then each element is converted into an integer using map(int, ...). After assignment, a = 3 and b = 4, so the result of a * b is 12.
What will be the result of this code if the user enters the value 42?
value = input("Enter something: ")
if value == 42:
print("Matched")
else:
print("Not matched")
The input() function returns a string. In the condition value == 42, we are comparing a string with an integer, which always results in False. Therefore, the else block is executed and "Not matched" is printed.
To make it work as expected, you should convert value to an integer using int(value).
What will the output be if the user enters hello?
text = input("Enter something: ")
print("You entered:", text[::-1])
The slicing operation text[::-1] reverses the input string. So if the user enters "hello", the reversed version "olleh" is printed.
Practicing Python Basic Input and Output? Don’t forget to test yourself later in
our
Python Quiz.
About This Exercise: Python – Basic Input and Output
Welcome to the Python Basic Input and Output exercises — an essential part of learning how to interact with users and the outside world using Python. These exercises are carefully designed to help you understand how Python reads data from the user and how it displays meaningful output on the screen. If you're just getting started with Python or brushing up on your skills, this section will help you build a strong foundation in basic I/O operations.
Input and output are the backbone of any interactive program. In this set of Python exercises, you'll explore how to use the built-in input() function to capture user input and the print() function to format and display output. From simple data capture to formatting multi-line text, these exercises will give you hands-on practice with both string inputs and numeric processing.
Along the way, you’ll also learn how to handle type conversions (like turning string input into integers or floats), manage user prompts, format output using f-strings and the format() method, and deal with common issues like input validation and unexpected input types.
These Python input/output exercises are built to simulate real-life situations — such as building interactive menus, creating basic calculators, prompting for user preferences, and producing clean, readable output. Mastering these concepts is especially useful for coding interviews, academic projects, and any program where user interaction plays a role.
If you're aiming for Python fluency, understanding how input and output work is non-negotiable. These foundational skills directly affect how you handle everything from command-line tools to web applications. This section also prepares you for more advanced topics like file I/O, error handling, and working with external APIs.
To reinforce your learning, be sure to try our interactive quizzes that test your grasp of Python input and output concepts. Combine these with regular hands-on practice, and you’ll soon be writing programs that not only process data effectively but also communicate results clearly and professionally.
Start exploring now and build the skills that will power every Python project you create. With consistent practice, you’ll soon handle user input and display output like a pro — the first step toward creating truly interactive Python applications.