Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using print() Parameters and Formatting
📖 Scenario: You are creating a simple program to display a shopping list clearly on the screen. You want to control how the items appear by using different print() options.
🎯 Goal: Learn how to use the print() function's parameters like sep and end, and how to format output with f-strings.
📋 What You'll Learn
Create a list of shopping items
Set a separator string for printing items
Use a for loop to print items with the separator
Format and print a summary line using an f-string
💡 Why This Matters
🌍 Real World
Formatting output clearly is important when showing lists or reports in programs, like shopping lists or logs.
💼 Career
Understanding print formatting helps in debugging and creating user-friendly console applications.
Progress0 / 4 steps
1
Create a list of shopping items
Create a list called shopping_list with these exact items: 'apples', 'bananas', 'carrots', 'dates'
Python
Hint
Use square brackets [] and separate items with commas.
2
Set a separator string
Create a variable called separator and set it to the string ', ' (a comma followed by a space).
Python
Hint
Use quotes around the comma and space.
3
Print items using the separator
Use a for loop with variable item to go through shopping_list. Inside the loop, use print() to print each item followed by the separator without moving to a new line after each print.
Python
Hint
Use the end parameter of print() to avoid new lines.
4
Print a formatted summary line
After the loop, print a new line with the text Total items: 4 using an f-string and the len() function on shopping_list.
Python
Hint
Use print(f"\nTotal items: {len(shopping_list)}") to start on a new line and show the count.
Practice
(1/5)
1. What does the sep parameter do in the print() function?
easy
A. It changes the separator between multiple items printed.
B. It changes the ending character of the printed line.
C. It formats numbers inside the print statement.
D. It pauses the program before printing.
Solution
Step 1: Understand the role of sep
The sep parameter defines what string is placed between multiple items printed.
Step 2: Compare with other parameters
end changes line ending, formatting is done by f-strings, and pausing is unrelated.
Final Answer:
It changes the separator between multiple items printed. -> Option A
Quick Check:
sep = separator [OK]
Hint: Remember: sep controls spacing between items [OK]
Common Mistakes:
Confusing sep with end parameter
Thinking sep formats numbers
Assuming sep pauses output
2. Which of the following is the correct syntax to print two words separated by a dash using print()?
easy
A. print('hello' - 'world')
B. print('hello' + 'world', sep='-')
C. print('hello', 'world', end='-')
D. print('hello', 'world', sep='-')
Solution
Step 1: Identify correct use of sep
Using sep='-' 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 D
Quick Check:
sep='-' joins words with dash [OK]
Hint: Use sep between items, not inside strings [OK]
Common Mistakes:
Using + operator with sep parameter
Confusing sep with end
Trying to subtract strings
3. What is the output of this code?
print('A', 'B', 'C', sep='*', end='!')
print('D')
medium
A. A*B*C!D
B. A B C ! D
C. A*B*C
D
D. A B C!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 A
Quick Check:
sep='*', end='!' joins and ends line [OK]
Hint: Remember end='' keeps output on same line [OK]
Common Mistakes:
Assuming default space separator
Thinking end adds newline
Ignoring end parameter effect
4. The following code prints 'Score:-10'. What is the fix to print 'Score: 10'?
print('Score:', score, sep='-')
Assuming score = 10.
medium
A. Convert score to string before printing.
B. Change sep to end parameter.
C. Remove sep parameter or use it correctly between multiple items.
D. Add parentheses around score.
Solution
Step 1: Understand sep usage
The sep='-' parameter places '-' between 'Score:' and score, resulting in 'Score:-10'.
Step 2: Identify the fix
To print 'Score: 10' with default space separator, remove the sep parameter.
Final Answer:
Remove sep parameter or use it correctly between multiple items. -> Option C
Quick Check:
remove sep -> default space [OK]
Hint: Use sep only when printing multiple items [OK]
Common Mistakes:
Confusing sep with end parameter
Trying to convert variables unnecessarily
Using sep with single string
5. How can you print the number 7.12345 rounded to 2 decimal places using print() and f-strings?
hard
A. print(f'{7.12345:2f}')
B. print(f'{7.12345:.2f}')
C. print(f'{7.12345:.2}')
D. print(f'{7.12345:.f2}')
Solution
Step 1: Understand f-string formatting
To round a float to 2 decimals, use :.2f inside 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 B
Quick Check:
Use .2f to round floats in f-strings [OK]
Hint: Use .2f inside f-string to round floats [OK]