Learning Goals
3 minBy the end of this lesson you can:
- Write helpful comments using
#and triple quotes. - Use
sep=andend=to shapeprint()output. - Add new lines and tabs inside a string with
\nand\t.
Warm-Up
5 minLast lesson we taught Python to speak with print(). We mixed words and numbers using commas.
Quick predictions — what do these two lines show?
print("a", "b", "c") print("a", "b", "c", sep="-")
Write your guesses on paper before scrolling on. The second line uses a magic word — we will meet sep in a moment.
Predicting first, then running, is how real programmers debug. It builds your "code sense" faster than just reading.
New Concept · Comments & Cleaner Output
12 minPart 1 · Comments — notes for humans
A comment is a line Python ignores. We use it to leave notes for ourselves and our teammates.
Single-line comments start with #:
# Print a friendly greeting print("Hello!")
The # can also sit at the end of a line of code:
print(2 * 60) # number of seconds in two minutes
For longer notes we can use triple quotes. They wrap text across many lines:
""" This program greets the user and shows today's lesson number. """ print("Welcome to lesson 2!")
Good comments explain why the code does something tricky. Don't restate what is already obvious from the code.
Part 2 · sep — the glue between items
When print() has many items, it joins them with a space by default. The keyword sep= changes the glue:
print("apple", "banana", "cherry") print("apple", "banana", "cherry", sep=", ") print("2026", "05", "07", sep="-")
Part 3 · end — what comes after
By default print() finishes with a new line. The keyword end= changes that:
print("Loading", end="...") print("done!")
Useful when we want two prints to appear on the same line.
Part 4 · Escape sequences inside a string
Some characters cannot be typed normally inside quotes. We use a backslash \ to escape them:
\n— start a new line\t— insert a tab\"— a quote inside a quoted string\\— a real backslash
print("Line 1\nLine 2") print("Name:\tAisyah") print("She said \"Hi!\"")
Worked Example · Three Ways to Print a Logo
12 minWe will print the same little logo three different ways. Each way uses fewer print() calls than the one before.
Way 1 · Three separate prints
Code
# A tiny logo, three lines, three prints print("=========================") print(" Advaslearning Hub") print("=========================")
Way 2 · One print, with \n
Same output, but using one print() and \n for new lines:
print("=========================\n Advaslearning Hub\n=========================")
Way 3 · One print, triple-quoted
Triple quotes keep the line breaks visible in our code — much easier to read:
print("""========================= Advaslearning Hub =========================""")
All three ways produce the same output:
Output
========================= Advaslearning Hub =========================
Bonus · sep and end together
print("apple", "banana", "cherry", sep=" • ", end=" 🍓\n")
The backslash \ only works inside quotes. print(\n) is an error — it must be print("\n").
Try It Yourself
13 minType each task into your editor. Run it. Fix any typos before moving on.
Open the my_week.py file from last lesson's homework. Add one short # comment above each print() line. Save the file.
Hint
A comment can be very short, like # show my favourite day. Keep it useful, not noisy.
Use a triple-quoted string to print a four-line poem about your weekend, using only one print().
Hint
Open with print(""", write four lines of poem, close with """). Real line breaks in your code become real line breaks in the output.
Use sep="\n" to print three favourite films, each on its own line, from one print() call.
Hint
print("Spider-Man", "Coco", "Up", sep="\n") — Python places \n between the items, so each one starts on a new line.
Mini-Challenge · Café Receipt
8 minBuild a small café receipt that uses exactly two print() statements.
Your receipt must include:
- A header with the shop name, address line, and a thank-you — built with one triple-quoted string.
- A list of at least four items with prices — built with one
print()usingsep="\n". - Use
\tto line up the prices. - Add a single comment at the top of your file explaining what the script does.
Stretch goal. Add a separator line of dashes by repeating a string: print("-" * 30).
Show one possible solution
# Prints a small Mamak Code receipt for the morning rush print("""=============================== Mamak Code — Jalan Bukit Bintang Terima kasih, come again! ===============================""") print("Teh Tarik\tRM2.50", "Kopi O\t\tRM2.00", "Roti Bakar\tRM3.00", "Kaya Toast\tRM2.80", sep="\n")
Recap
3 minWe taught Python to write tidier output and to leave notes for humans. sep, end, and \n let one print() do the work of many.
Vocabulary Card
- comment
- A note for humans. Single line with
#; multi-line with"""…""". - sep
- The glue between items inside a
print(). Default is one space. - end
- The string Python adds after a print. Default is a new line.
- escape sequence
- A backslash trick inside a string.
\n= new line.\t= tab.
Homework
4 minCreate a new file called menu.py. Inside, build a five-item menu for a pretend restaurant.
Rules:
- Start with a top comment that names your restaurant.
- Print a header line of dashes using
"-" * 30. - Print the five items with
\taligning the prices.
Bring it to the next lesson — we will read each other's menus aloud!
Sample · menu.py
# menu.py — pretend restaurant called Bytes & Bites in Penang print("-" * 30) print(" Bytes & Bites Menu") print("-" * 30) print("Nasi Lemak\tRM6.50", "Char Kway Teow\tRM7.50", "Mee Goreng\tRM6.00", "Cendol\t\tRM4.50", "Air Sirap\tRM2.50", sep="\n")
Tab widths vary across editors. Use one or two \t per line until the prices line up. Yours can be totally different — different shop name, different items, just stick to the rules.