If a user enters the number 25 using the input() function, what will be the data type of the stored value?
The input() function always returns user input as a string, regardless of what the user types.
Example:
x = input("Enter a number: ") # User enters 25
print(type(x)) # <class 'str'>
Why others are wrong:
Option 1 (int) → Only true if we explicitly convert using int(input()).
Option 2 (float) → Only true if converted using float(input()).
Option 4 (bool) → Not the default type for input values.
So even if the user enters 25, it will be stored as "25" (a string).
What will be the output of the following code?
x = int(input("Enter a number: "))
print(x * 2)
If the user enters 7, what will be displayed?
The input() function reads user input as a string.
Wrapping it with int() converts the input into an integer.
For user input 7:
x = int("7") → x = 7
print(x * 2) → 14
Why others are wrong:
Option 1: Would only be correct if code printed x, not x*2.
Option 3:"7" * 2 (string repetition) would give "77", but here it’s converted to int.
Option 4: No error occurs since 7 is valid input for int().
Which of the following print() statements will produce the output Hello-Python-World?
The sep parameter in print() specifies the string inserted between multiple arguments.
Example:
print("Hello", "Python", "World", sep="-")
Output:
Hello-Python-World
Why others are wrong:
Option 1: Would print HelloPythonWorld without separators.
Option 2: Uses default space separator → Hello Python World.
Option 3:end changes the ending character, not the separator.
Using sep is important for custom formatting of output without manually concatenating strings.
Which of the following statements correctly demonstrates formatted string (f-string) output in Python?
F-strings (introduced in Python 3.6) allow embedding expressions inside string literals using {} with a leading f.
Example:
name = "Alice"
print(f"Hello, {name}!") # Output: Hello, Alice!
Why others are wrong:
Option 2: Old-style formatting using %. Valid, but not f-string.
Option 3:.format() method, also valid, but f-string is more modern and concise.
Option 4:{name} is treated literally, not evaluated.
F-strings are preferred for readability and efficiency when formatting strings in Python.
What will be the output of the following code?
x = 5
y = 3
print("Sum =", x + y, "Difference =", x - y)
By default, print()separates arguments with a space (sep=" ").
The code passes multiple arguments: "Sum =", x + y, "Difference =", x - y.
Evaluating:
x + y = 8
x - y = 2
Output:
Sum = 8 Difference = 2
Why others are wrong:
Option 1: No spaces appear between arguments → incorrect.
Option 3: Uses colons and commas, which the code does not have.
Option 4: Only numbers are printed, but the code includes labels.
This illustrates default argument separation and multiple arguments in print().
Which of the following statements correctly converts user input into a floating-point number?
input() always returns a string. To use numeric values, conversion is required.
float(input(...)) converts the string input into a floating-point number.
Example:
x = float(input("Enter a number: ")) # User enters 2.5
print(x + 1) # Output: 3.5
Why others are wrong:
Option 1: Works only for literal "3.14", not user input.
Option 3: Converts input to int, not float.
Option 4: Stores input as string, not numeric type.
This ensures that numeric calculations can be performed on user input.
Which of the following code snippets correctly reads multiple numbers in a single line separated by spaces?
input().split() returns a list of strings from a single line of input.
map(int, ...) converts each string to an integer.
Using unpacking a, b, c = ... assigns values to individual variables.
Example:
Input: 1 2 3
a, b, c = map(int, input().split())
print(a, b, c) # Output: 1 2 3
Why others are wrong:
Option 1: Values remain strings; no type conversion.
Option 2: Cannot convert list directly to int.
Option 3:int(input()) expects a single number, not multiple values.
This is important for competitive programming and efficient input handling.
Which of the following demonstrates reading multi-line input until an empty line is entered?
Using a while loop, we can read multiple lines until the user provides an empty string ("") to stop.
Each line is appended to a list for later processing.
Example:
Input:
Hello
Python
World
Output stored in lines: ['Hello', 'Python', 'World']
Why others are wrong:
Option 2:input().split("\n") only works if input contains newline characters already; input() reads a single line.
Option 3:map(input()) is invalid syntax.
Option 4: Multiplying a string does not read multiple lines.
This method is important for handling unknown number of input lines in scripts or programs.
Which escape sequence in Python is used to insert a tab in the output?
In Python, escape sequences allow special characters to be included in strings.
\t inserts a horizontal tab, creating spacing between content.
Example:
print("Name\tAge")
print("Alice\t25")
Output:
Name Age
Alice 25
Why others are wrong:
Option 1 (\n) → Inserts a newline, not a tab.
Option 2 (\r) → Carriage return; moves cursor to start of line.
Option 4 (\\) → Prints a backslash character.
Escape sequences are important for formatting output neatly.
Which of the following statements correctly writes output to a file instead of the console?
The print() function can redirect output to a file using the file parameter.
Using a with statement ensures the file is properly opened and closed.
Example:
with open("output.txt", "w") as f:
print("Hello World", file=f)
This writes Hello World into output.txt.
Why others are wrong:
Option 2: Incorrect syntax; > is not used with print() in Python.
Option 3:write() exists as a file method, but syntax is wrong.
Option 4: No output() function exists in Python for file writing.
This method is useful for logging, saving results, or generating reports in Python programs.
Which of the following removes leading and trailing spaces from user input?
The strip() method removes leading and trailing whitespace from a string.
This is useful when users may accidentally add spaces around input.
Example:
name = input("Enter your name: ").strip()
Input: " Alice " → Stored as "Alice".
Why others are wrong:
Option 2 (remove()) → Not a string method in Python.
Option 3 (trim()) → Exists in other languages (like Java), not Python.
Option 4 (clear()) → Clears lists or dictionaries, not strings.
This ensures clean input handling, which is crucial before processing or storing data.
Which of the following is faster for reading large input in Python?
For large or multiple lines of input, sys.stdin.readline() is more efficient than input().
input() does some additional processing (like stripping newline), which can slow down programs in competitive programming scenarios.
Example:
import sys
n = int(sys.stdin.readline())
This reads a line and can handle large datasets faster.
Why others are wrong:
Option 1:input() is slower for heavy input.
Option 3:print() is for output, not input.
Option 4: Opening a file is unrelated to reading standard input.
This is crucial for time-sensitive programs requiring fast input handling.
Which of the following functions returns the official string representation of an object, useful for debugging?
repr() returns a string representation of an object that is meant to be unambiguous and useful for debugging.
It often includes quotes for strings and shows the object’s type clearly.
Example:
s = "Hello"
print(str(s)) # Hello
print(repr(s)) # 'Hello'
Why others are wrong:
Option 1 (str()) → Provides readable output, but not necessarily unambiguous.
Option 2 (print()) → Displays output but does not return a string.
Option 4 (format()) → Used for string formatting, not debugging representation.
This distinction is important for logging and debugging programs.
Which of the following is the most efficient way to print a large list of strings separated by spaces in Python?
Using " ".join(my_list)concatenates all elements into a single string before printing, which is more efficient than multiple print() calls in a loop.
This is especially important when printing large datasets.
Example:
my_list = ["Python", "is", "fun"]
print(" ".join(my_list)) # Output: Python is fun
Why others are wrong:
Option 1: Works but slower for large lists due to multiple print calls.
Option 2: Prints the list with brackets and commas → not just elements.
Option 4: Can work but manually managing sep may be less readable and efficient.
Using join() is the preferred Pythonic way to efficiently print large sequences.
Which of the following demonstrates aligning and padding output using f-strings?
F-strings allow alignment and padding with a fill character:
< → left-align
> → right-align
^ → center-align
[fill_char]^width → centers text and fills extra space with fill_char
Example:
print(f"{'Hello':!^10}") # Output: !!Hello!!!
"Hello" is centered in a 10-character wide field with ! filling extra space.
Why others are wrong:
Option 1: Right-aligns but uses spaces as fill.
Option 2: Left-aligns with spaces.
Option 3: Center-aligns with spaces.
This is useful for neatly formatted tables, reports, and aligned output in Python programs.
Which of the following statements correctly demonstrates reading input safely until EOF in Python?
Explanation detail
sys.stdin allows reading input until EOF (End of File) efficiently.
Each line can be processed inside the loop, and strip() removes trailing newline characters.
Example usage (useful in competitive programming):
import sys
for line in sys.stdin:
print(line.strip())
This will continue reading until no more input is available (EOF).
Why others are wrong:
Option 1: Infinite loop; stops only if manually interrupted.
Option 2:x is undefined in print(x); also stops only on empty line.
Option 3: Iterates characters of a single input line, not multiple lines.
This method is essential for efficient and safe input handling when reading unknown or large amounts of data.
Quick Recap of Python Basic Input and Output Concepts
If you are not clear on the concepts of Basic Input and Output, you can quickly review them here before practicing the exercises. This recap highlights the essential points and logic to help you solve problems confidently.
What Is Output in Python?
You use the built-in print() function to display output on the screen.
print() can take multiple items (strings, variables, expressions) and separate them with commas. By default, it separates them with a space.
You can control how things are printed using keyword parameters:
sep: defines what string is put between the items.
end: defines what is added at the end (instead of the default newline \n).
Example:
first = "Python"
second = "Rocks"
print(first, second, sep=" -- ", end="!\n")
# Output: Python -- Rocks!
What Is Input in Python?
The input() function pauses your program and waits for the user to type something, then returns that as a string.
You can optionally provide a prompt inside input(...), which will be shown to the user before they type.
Example:
city = input("Where do you live? ")
print("You live in", city)
Converting User Input (Casting) in Python
Since input() always returns a string, you often need to convert (“cast”) that string into another type if you want to do calculations.
Use int() to convert to integer, float() to convert to decimal numbers.
Example:
age_str = input("Enter your age: ")
age = int(age_str)
print("In five years, you will be", age + 5)
Be careful: if the user types something that can’t be converted (like “abc”), it will cause an error at runtime. Good programs should check or validate input before casting.
Taking Multiple Inputs in Python
You can read more than one value from a single input() call by using .split() to divide the input string into parts.
Example:
x, y = input("Enter two numbers separated by space: ").split()
x = int(x)
y = int(y)
print("First:", x, "Second:", y)
Advanced print() Parameters & Behavior in Python
print() supports more than just sep and end: you can also redirect output with file or flush the output buffer with flush.
Example of using end in a loop, to print items on the same line:
for i in range(5):
print(i, end=" ")
print() # This moves to the next line afterward
# Output: 0 1 2 3 4
Best Practices & Tips for Input & Output in Python
Always provide a clear prompt in input() so users understand what they need to type.
Validate or sanitize user input before converting it to a number to avoid runtime errors.
Use formatting (like f-strings) to make your print output cleaner and more readable.
Be careful with long or complex inputs — splitting, converting, and handling them properly makes your program more robust.
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 Solviyo’s Python Basic Input and Output exercises. These exercises are designed to help beginners understand how Python programs interact with users, allowing you to both collect information and display meaningful results. Input and output are fundamental skills that form the backbone of many programs, and practicing them early will build your confidence and coding proficiency.
What You Will Learn
In this set of exercises, you will explore essential input and output concepts, including:
Using the input() function to read data from users.
Displaying results with the print() function in a readable and professional way.
Formatting output using f-strings and other string formatting techniques.
Combining multiple variables into a single output.
Performing calculations with numeric input and handling type conversion between strings, integers, and floats.
Designing interactive, real-world scenarios that make learning practical and relevant.
Best practices for clean, readable, and maintainable input/output code.
Why Learning Python Input and Output Matters
Mastering input and output is a key step in becoming an effective Python programmer. These skills allow your programs to interact with users, take dynamic data, and display results in a clear and structured way. A solid understanding of I/O also prepares you for advanced topics like file handling, user-driven applications, and error handling.
Start Your Python Journey with Input and Output
By practicing these exercises, you will gain the confidence to handle user input, display formatted results, and build simple interactive programs. This foundation is essential for progressing to more complex Python topics and real-world projects. Start working on Python Basic Input and Output exercises today, and take the first step toward creating functional, user-friendly programs.
Need a Quick Refresher?
Jump back to the Python Cheat Sheet to review concepts before solving more challenges.