print() parameters and formatting in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to run print() changes when we use different parameters and formatting.
How does the number of items or formatting affect the time print() needs?
Analyze the time complexity of the following code snippet.
for i in range(n):
print(f"Number: {i}", end=' ', flush=False)
print()
This code prints numbers from 0 to n-1 on the same line with a space, then moves to a new line.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The print() function inside the loop.
- How many times: It runs once for each number from 0 to n-1, so n times.
As n grows, the number of print calls grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print calls |
| 100 | 100 print calls |
| 1000 | 1000 print calls |
Pattern observation: The time grows directly with the number of items printed.
Time Complexity: O(n)
This means the time to print grows in a straight line as you print more items.
[X] Wrong: "Changing print formatting or parameters does not affect time complexity at all."
[OK] Correct: While formatting itself is fast, more complex formatting or flushing output can add small extra time per print, but the main time still grows with how many times print runs.
Understanding how print and formatting scale helps you think about how programs handle output efficiently, a useful skill for writing clear and fast code.
"What if we added flush=True inside the print call? How would the time complexity change?"
Practice
sep parameter do in the print() function?Solution
Step 1: Understand the role of
Thesepsepparameter defines what string is placed between multiple items printed.Step 2: Compare with other parameters
endchanges line ending, formatting is done by f-strings, and pausing is unrelated.Final Answer:
It changes the separator between multiple items printed. -> Option AQuick Check:
sep = separator [OK]
- Confusing sep with end parameter
- Thinking sep formats numbers
- Assuming sep pauses output
print()?Solution
Step 1: Identify correct use of
Usingsepsep='-'between multiple arguments prints them separated by a dash.Step 2: Check syntax correctness
print('hello', 'world', sep='-') uses correct syntax. print('hello' + 'world', sep='-') concatenates strings before printing, ignoring sep. print('hello', 'world', end='-') uses end, which changes line ending, not separator. print('hello' - 'world') is invalid syntax.Final Answer:
print('hello', 'world', sep='-') -> Option DQuick Check:
sep='-' joins words with dash [OK]
- Using + operator with sep parameter
- Confusing sep with end
- Trying to subtract strings
print('A', 'B', 'C', sep='*', end='!')
print('D')Solution
Step 1: Analyze first print()
It prints 'A', 'B', 'C' separated by '*', ending with '!'. So output is 'A*B*C!'Step 2: Analyze second print()
It prints 'D' on the same line because first print ended with '!' (no newline).Final Answer:
A*B*C!D -> Option AQuick Check:
sep='*', end='!' joins and ends line [OK]
- Assuming default space separator
- Thinking end adds newline
- Ignoring end parameter effect
print('Score:', score, sep='-')Assuming
score = 10.Solution
Step 1: Understand sep usage
Thesep='-'parameter places '-' between 'Score:' andscore, resulting in 'Score:-10'.Step 2: Identify the fix
To print 'Score: 10' with default space separator, remove thesepparameter.Final Answer:
Remove sep parameter or use it correctly between multiple items. -> Option CQuick Check:
remove sep -> default space [OK]
- Confusing sep with end parameter
- Trying to convert variables unnecessarily
- Using sep with single string
print() and f-strings?Solution
Step 1: Understand f-string formatting
To round a float to 2 decimals, use:.2finside the braces.Step 2: Check each option
print(f'{7.12345:.2f}') uses correct syntax{value:.2f}. print(f'{7.12345:2f}') misses the dot before 2. print(f'{7.12345:.2}') uses wrong format specifier. print(f'{7.12345:.f2}') has incorrect order.Final Answer:
print(f'{7.12345:.2f}') -> Option BQuick Check:
Use .2f to round floats in f-strings [OK]
- Omitting the dot before precision
- Using wrong format specifiers
- Mixing order of letters and numbers
