Bird
Raised Fist0
Pythonprogramming~10 mins

print() parameters and formatting in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - print() parameters and formatting
Start print() call
Evaluate arguments
Apply sep between args
Add end string
Send output to console
print() ends
This flow shows how print() takes arguments, joins them with sep, adds end, then outputs to console.
Execution Sample
Python
print('Hello', 'World', sep='-', end='!\n')
Prints 'Hello-World!' by joining with '-' and ending with '!\n'.
Execution Table
StepActionArguments Evaluatedsep Usedend UsedOutput Produced
1Start print() call('Hello', 'World')-!\n
2Join arguments with sep'Hello' + '-' + 'World'-!\nHello-World
3Add end stringN/A-!\nHello-World!
4Send output to consoleN/A-!\nHello-World! (with newline)
5print() endsN/A-!\nN/A
💡 print() finishes after outputting the joined string with end appended.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
args('Hello', 'World')('Hello', 'World')('Hello', 'World')('Hello', 'World')
sep'-''-''-''-'
end'!\n''!\n''!\n''!\n'
output_string'''Hello-World''Hello-World!\n''Hello-World!\n'
Key Moments - 3 Insights
Why does print() add a space by default between arguments?
By default, print() uses sep=' ' to join arguments. In the execution_table step 2, you see sep is '-' because we set it, but if not set, it is a space.
What happens if we don't specify end parameter?
If end is not given, print() uses '\n' (newline) by default. In the table, end='!\n' is used explicitly, but normally it ends with just '\n'.
Can print() output multiple arguments without converting them manually?
Yes, print() converts all arguments to strings automatically before joining with sep, as shown in step 2 where arguments are joined.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the output string before adding end?
AHello-World
BHello World
CHelloWorld
DHello!World
💡 Hint
Check the 'Output Produced' column at step 2 in execution_table.
At which step does print() add the end string to the output?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table where end is applied.
If we change sep to ',' and end to '.' what will be the final output string?
AHello-World!
BHello World.
CHello,World.
DHello, World.
💡 Hint
Refer to variable_tracker 'sep' and 'end' values and how they affect output_string.
Concept Snapshot
print(*objects, sep=' ', end='\n')
- Joins objects with sep string (default space)
- Ends output with end string (default newline)
- Converts all objects to strings automatically
- Useful for formatting output simply
Full Transcript
The print() function in Python takes any number of arguments and prints them to the console. It joins all arguments by a separator string called sep, which defaults to a space. After joining, it adds an end string, which defaults to a newline character. You can change sep and end to customize how the output looks. For example, print('Hello', 'World', sep='-', end='!\n') prints Hello-World! and moves to the next line. This visual trace shows each step: starting the call, joining arguments with sep, adding end, and sending output to the console. Variables like args, sep, end, and output_string change as the function runs. Remember, print() automatically converts all arguments to strings before joining. This makes it easy to print multiple values without manual conversion.

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

  1. Step 1: Understand the role of sep

    The sep parameter defines what string is placed between multiple items printed.
  2. Step 2: Compare with other parameters

    end changes line ending, formatting is done by f-strings, and pausing is unrelated.
  3. Final Answer:

    It changes the separator between multiple items printed. -> Option A
  4. 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

  1. Step 1: Identify correct use of sep

    Using sep='-' between multiple arguments prints them separated by a dash.
  2. 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.
  3. Final Answer:

    print('hello', 'world', sep='-') -> Option D
  4. 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

  1. Step 1: Analyze first print()

    It prints 'A', 'B', 'C' separated by '*', ending with '!'. So output is 'A*B*C!'
  2. Step 2: Analyze second print()

    It prints 'D' on the same line because first print ended with '!' (no newline).
  3. Final Answer:

    A*B*C!D -> Option A
  4. 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

  1. Step 1: Understand sep usage

    The sep='-' parameter places '-' between 'Score:' and score, resulting in 'Score:-10'.
  2. Step 2: Identify the fix

    To print 'Score: 10' with default space separator, remove the sep parameter.
  3. Final Answer:

    Remove sep parameter or use it correctly between multiple items. -> Option C
  4. 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

  1. Step 1: Understand f-string formatting

    To round a float to 2 decimals, use :.2f inside the braces.
  2. 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.
  3. Final Answer:

    print(f'{7.12345:.2f}') -> Option B
  4. Quick 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