0
0
Pythonprogramming~20 mins

print() parameters and formatting in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Print Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of print() with sep and end parameters
What is the output of this Python code?
print('A', 'B', 'C', sep='-', end='!')
print('D')
Python
print('A', 'B', 'C', sep='-', end='!')
print('D')
AA B C!D
BA B C\nD
CA-B-C\nD
DA-B-C!D
Attempts:
2 left
💡 Hint
Remember that sep changes the separator between items and end changes what is printed at the end.
Predict Output
intermediate
2:00remaining
Using flush parameter in print()
What does this code print?
import sys
print('Hello', flush=True)
print('World', flush=False)
Python
import sys
print('Hello', flush=True)
print('World', flush=False)
AHello\nWorld\n
BHelloWorld
CHello\nWorld
DHello World
Attempts:
2 left
💡 Hint
The flush parameter forces the output buffer to write immediately.
Predict Output
advanced
2:00remaining
Formatted string with print() and f-string
What is the output of this code?
name = 'Sam'
age = 7
print(f'{name} is {age} years old.')
Python
name = 'Sam'
age = 7
print(f'{name} is {age} years old.')
A{name} is {age} years old.
Bname is age years old.
CSam is 7 years old.
DSam is age years old.
Attempts:
2 left
💡 Hint
f-strings replace variables inside curly braces with their values.
Predict Output
advanced
2:00remaining
Using print() with multiple end parameters
What is the output of this code?
print('X', end='-')
print('Y', end='*')
print('Z')
Python
print('X', end='-')
print('Y', end='*')
print('Z')
AX-Y*Z\n
BX-Y-Z\n
CX Y Z\n
DX-Y*Z
Attempts:
2 left
💡 Hint
Each print uses its own end parameter to decide what to print after the content.
🧠 Conceptual
expert
2:00remaining
Understanding print() with file parameter
What happens when you run this code?
import sys
print('Error message', file=sys.stderr)
Python
import sys
print('Error message', file=sys.stderr)
APrints 'Error message' to the standard output (stdout).
BPrints 'Error message' to the error output stream (stderr).
CRaises a TypeError because file=sys.stderr is invalid.
DPrints nothing because stderr is not connected.
Attempts:
2 left
💡 Hint
The file parameter directs print output to a specific stream.