The print() function shows information on the screen. You can control how it looks and what it shows by using its parameters.
print() parameters and formatting in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
print(*objects, sep=' ', end='\n', file=None, flush=False)
*objects means you can print many things separated by commas.
sep sets what goes between items (default is space).
end sets what prints at the end (default is new line).
Examples
Python
print('Hello', 'World')
Python
print('Hello', 'World', sep='-')
Python
print('Hello', end='!') print('World')
Python
print(f'Number: {3.14159:.2f}')
Sample Program
This program shows how to use sep to separate items with commas, end to avoid new line, and f-string formatting to round a number.
Python
print('Apples', 'Oranges', 'Bananas', sep=', ') print('Loading', end='...') print('Done!') value = 7.12345 print(f'Value rounded: {value:.2f}')
Important Notes
You can print many items by separating them with commas inside print().
Use sep to change the space between items, like commas or dashes.
Use end to control what happens after printing, like staying on the same line.
Summary
print() shows information on the screen.
You can change spacing with sep and line endings with end.
Use f-strings to format numbers or text inside print().
Practice
1. What does the
sep parameter do in the print() function?easy
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]
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
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]
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
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]
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'?
Assuming
print('Score:', score, sep='-')Assuming
score = 10.medium
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]
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
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]
Hint: Use .2f inside f-string to round floats [OK]
Common Mistakes:
- Omitting the dot before precision
- Using wrong format specifiers
- Mixing order of letters and numbers
