Output & Escape Sequences
Every program needs to communicate with the user. In Python,
print() is the main tool for displaying information. This lesson focuses on
organising output with sep and end, and using escape sequences to
control formatting.
Learning Objectives
- 11.1.1.1 Organize data output
- 11.1.1.2 Use the escape sequences with data output
Video Explanation
Watch a visual walkthrough of Python output and formatting.
Conceptual Anchor
The Billboard Analogy
Think of print() as a digital billboard. You choose what to
display, how items are separated using sep, and
what comes after the output using end. Escape sequences
are formatting controls that create new lines, tabs or special characters.
Anatomy of print()
print("A", "B", "C",
sep="-",
end="!")
A-B-C!
"A", "B", "C"
The objects that Python displays.
sep="-"
Text inserted between multiple values. Default: one space.
end="!"
Text added after all values. Default: newline \n.
# General form
print(value1, value2, ..., sep=' ', end='\n')
print("Hello, World!")
print("Name:", "Ali")
print("2026", "09", "30", sep="-")
print("Loading", end="...")
print("Done!")
print() # blank lineOutput Predictor
Predict the exact output before revealing it. Pay
attention to spaces, line breaks, sep and end.
Print Playground
Change the values, separator and ending. The preview models one
print() statement.
Escape Sequences
An escape sequence begins with a backslash and represents a special character inside a string.
| Escape Sequence | Meaning | Example Output |
|---|---|---|
\n |
New line | Line 1, then Line 2 on the next line |
\t |
Horizontal tab / next tab stop | Useful for tabbed spacing |
\\ |
Backslash character | C:\Users\ |
\' |
Single quote | It's |
\" |
Double quote | He said "Hi" |
Escape Sequence Explorer
About \t
\t inserts a horizontal tab and advances to the next tab stop. Its visible
width depends on the terminal, editor or environment, so do not treat it as “exactly
eight spaces”.
Code vs Output
Study how tiny changes affect output. The last examples show common errors.
print("A", "B", "C")
A B C
print("A", "B", "C", sep="")
ABC
print("A\nB\nC")
A B C
print("A", "B", "C", sep="\n")
A B C
print("Loading", end="...")
print("Done")
Loading...Done
print("A", "B", sep=":", end=" | ")
print("C")
A:B | C
print("Name\tScore")
print("Ali\t95")
Name Score Ali 95
print("Top")
print()
print("Bottom")
Top Bottom
print("Score:" 95)
SyntaxError
print('It's raining')
SyntaxError
print("C:\new\test")
C: ew est
age = 16
print("Age: " + age)
TypeError
File Paths: Escaped vs Raw Strings
Windows-style paths can accidentally contain \n and
\t. Two common solutions are shown below.
Extension
Raw strings are useful when text contains many backslashes, but the core objective remains understanding escape sequences.
Worked Examples
Fix the Output
Find the most important problem, then choose the best correction.
Pitfalls & Common Errors
Forgetting Escape in File Paths
print("C:\new\test") interprets \n as a newline and
\t as a tab. Use "C:\\new\\test" or, where appropriate,
r"C:\new\test".
Mixing Quote Types Incorrectly
print('It's raining') causes a syntax error. Use
print("It's raining") or print('It\'s raining').
Missing Comma Between Arguments
print("Score:" 95) is invalid. Use print("Score:", 95).
Concatenating Text and Numbers Directly
"Age: " + 16 causes a TypeError. Use
print("Age:", 16) or convert the number with str().
Forgetting What end Does
end="" or end=" " means the next print() begins on
the same line.
Pro-Tips for Exams
Output Questions
- Write the output exactly, including spaces, punctuation and line breaks.
- Default
sep=' 'is one space. - Default
end='\n'moves to the next line. print()with no values outputs only its ending, so the default creates a blank line.\tmoves to a tab stop; do not assume exactly eight spaces.- With several
print()calls, trace where the previousendleaves the cursor.
Graded Tasks
Match \n, \t, \\, \',
\" with their meanings.
State the exact output of print("A", "B", "C", sep="-").
Use sep to print 2026-09-30 from three separate values.
Use end so the numbers 1 to 5 are printed on the same line.
Print the path C:\Users\student\Documents\homework.py correctly.
Write the exact output of
print("A", "B", sep=":", end=" | "); print("C\nD").
Create a formatted receipt using print(), \t,
\n, sep and end. Include at least three
items, prices and a total.
Exam Practice
Q1: Write the exact output.
[2]print("Red", "Green", "Blue", sep="/")
Reveal marking point
Q2: Write the exact output.
[3]print("A", "B", sep=":", end=" | ")print("C\nD")
Reveal marking points
Q3: Explain why print("C:\new\test") does not display
the intended path. [2]
Reveal marking points
Q4: Correct print('It's ready'). [1]
Reveal marking point
Self-Check Quiz
sep?' '.\t do?print("A", "B", "C", sep="-", end="!")?A-B-C!
\\.
print("Hello\nWorld")?Hello is
printed on one line and World on the next.