Part 01 / Topic 7 of 7
print(), comments and indentation
print() shows output. You can pass many values, and change the separator (sep) and line ending (end).
Comments start with #. Python ignores them. Use them to explain why.
Indentation (spaces at the start of a line) is part of Python's grammar. Use 4 spaces. Code inside a block (like if, which you will learn in Part 3) must be indented.
Real-world example: Print a small welcome banner for a shop.
# Print many values in one call
print("Python", "is", "fun") # Python is fun
print("26", "11", "2026", sep="/") # 26/11/2026
print("Loading", end="...")
print("done") # Loading...done
"""A triple-quoted string can span many lines.
Here, in the middle of the code, it is just a string used like a comment.
Only at the very top of a file or function is it called a docstring."""
# `if` runs the indented line only when the condition is True (Part 3)
shop_open = True
if shop_open:
print("Welcome to Sharma General Store") # 4 spaces before print
# Welcome to Sharma General Store
| Escape | Meaning | Example | Output |
|---|---|---|---|
\n |
New line | print("A\nB") |
A, then B on next line |
\t |
Tab | print("A\tB") |
A, a tab space, then B |
\\ |
Backslash | print("C:\\temp") |
C:\temp |
\" |
Quote | print("He said \"Hi\"") |
He said "Hi" |
๐ Going deeper: The full signature is
print(*values, sep=" ", end="\n", file=None, flush=False)โfile=Nonemeans "write to the screen" (sys.stdout).print()turns each value into text withstr()first. Useflush=Truewhen you need output to appear at once (for example, progress messages in a slow script).
๐งช Quick quiz โ answer from memory, then check.
- What does
print("a", "b", sep="-")show? - What starts a comment?
- How many spaces does this course use for one level of indentation?
Show the answers
a-b#โ Python ignores the rest of the line.- 4 spaces.
Done here? The practice set for Part 1 โ 6 exercises with solutions โ is on the part page.