Output & Escape Sequences
Every program needs to communicate with the user. In Python, the
print() function is your primary tool for displaying information. This lesson covers
how to organize data output and use special 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 this topic
Conceptual Anchor
The Billboard Analogy
Think of print() as a digital billboard. You choose what to display
(the message),
how items are separated (the sep parameter), and what
comes at the end
(the end parameter). Escape sequences are like formatting controls — they tell the
billboard
to start a new line, insert a tab space, or display special
characters like quotes.
Rules & Theory
The print() Function
# Basic syntax
print(value1, value2, ..., sep=' ', end='\n')
# Simple output
print("Hello, World!") # Hello, World!
# Multiple values — separated by space by default
print("Name:", "Ali") # Name: Ali
print("Age:", 16) # Age: 16
# Changing the separator
print("2025", "02", "19", sep="-") # 2025-02-19
print("A", "B", "C", sep=" → ") # A → B → C
# Changing the end character
print("Loading", end="...") # Loading...
print("Done!") # Done! (on same line)
# Empty print = blank line
print() # (blank line)Escape Sequences
| Escape Sequence | Meaning | Example Output |
|---|---|---|
\n |
New line | Line 1↵Line 2 |
\t |
Tab (8 spaces) | Name Score |
\\ |
Backslash character | C:\Users\ |
\' |
Single quote | It's |
\" |
Double quote | He said "Hi" |
# Escape sequences in action
print("Line 1\nLine 2\nLine 3")
# Line 1
# Line 2
# Line 3
print("Name\tScore\tGrade")
print("Ali\t95\tA")
print("Dana\t87\tB")
# Name Score Grade
# Ali 95 A
# Dana 87 B
print("Path: C:\\Users\\student\\file.py")
# Path: C:\Users\student\file.py
print('He said: "Hello!"') # He said: "Hello!"
print("It\'s a sunny day") # It's a sunny dayOrganizing Output
Use \t to create aligned columns. Use \n to break long messages across
lines. Combine sep and end with loops for formatted tables.
Worked Examples
1 Formatting a Student Report
# Organized output using sep and escape sequences
name = "Aisha"
grade = 11
score = 92.5
print("=" * 30)
print("STUDENT REPORT")
print("=" * 30)
print("Name:", name)
print("Grade:", grade)
print("Score:", score)
print("=" * 30)
# Output:
# ==============================
# STUDENT REPORT
# ==============================
# Name: Aisha
# Grade: 11
# Score: 92.5
# ==============================2 Building a Table with Tabs
# Using \t to create aligned columns
print("Product\tPrice\tQty")
print("-" * 30)
print("Apple\t$1.20\t50")
print("Banana\t$0.80\t120")
print("Orange\t$1.50\t75")
# Output:
# Product Price Qty
# ------------------------------
# Apple $1.20 50
# Banana $0.80 120
# Orange $1.50 753 Printing on the Same Line
# Using end parameter to stay on the same line
print("Counting: ", end="")
for i in range(1, 6):
print(i, end=" ")
print() # Move to next line
# Output:
# Counting: 1 2 3 4 54 Multi-line Art with Escape Sequences
# Combining \n for multi-line output
print(" *\n ***\n*****\n ***\n *")
# Output:
# *
# ***
# *****
# ***
# *Pitfalls & Common Errors
Forgetting Escape in File Paths
Writing print("C:\new\test") will interpret \n as a newline and
\t as a tab! Use \\ or a raw string r"C:\new\test".
Mixing Quote Types
print('It's raining') causes a SyntaxError because the apostrophe ends the string
early. Fix: print("It's raining") or print('It\'s raining').
Missing Separator between Arguments
print("Score:" 95) is wrong — you need a comma between arguments:
print("Score:", 95).
Pro-Tips for Exams
Output Questions
- When tracing
print()withsepandend, write out the exact output character by character - Remember: default
sep=' '(space) and defaultend='\n'(newline) print()with no arguments just prints a blank line- Count the
\tstops carefully — they align to 8-character boundaries
Graded Tasks
List all 5 escape sequences from this lesson and give one example for each.
Explain the difference between print("A", "B") and
print("A", "B", sep=""). What does each output?
Write a program that prints a multiplication table for 5 (from 5×1 to 5×10) using
\t for alignment.
Print the following path correctly: C:\Users\student\Documents\homework.py
Create a formatted "receipt" using print() with \t,
\n, sep, and end. Include at least 3 items with
prices and a total.
Self-Check Quiz
sep parameter in
print()?
\t do in a string?print("A", "B", "C", sep="-", end="!")?
print("Hello\nWorld")?