FREE GUIDE
Python print(): everything it does, and the eight mistakes that break it
Works on Python 3.7 and newer. Every example can be pasted straight into a file and run.
print() is the first function every Python learner meets, and the one most people never finish learning. It takes any number of values, turns each into text with str(), joins them with a separator, adds an ending, and sends the whole thing to a stream. Four knobs, and almost all beginner confusion comes from not knowing which knob they are turning.
The signature
print(*values, sep=" ", end="\n", file=None, flush=False)
| Parameter | Default | What it changes |
|---|---|---|
*values | — | Any number of items. Each one is converted with str(), so you can pass numbers, lists, objects. |
sep | " " | The string put between the values. |
end | "\n" | What comes after the last value. This is why print moves to a new line. |
file | sys.stdout | Where the text goes — pass an open file, or sys.stderr. |
flush | False | Write it now instead of waiting for the buffer. Matters when you print progress. |
sep: joining several values
print("a", "b", "c") # a b c
print("a", "b", "c", sep=" - ") # a - b - c
print(*[1, 2, 3], sep=", ") # 1, 2, 3
The last line is the idiomatic way to unpack a list into a readable line. print([1, 2, 3]) prints [1, 2, 3] with brackets; print(*[1, 2, 3], sep=", ") prints 1, 2, 3.
end: staying on the same line
for i in range(1, 5):
print(i, end=" ")
# 1 2 3 4
Set end="" when you are building one line across several calls — a progress display, for example. For prompts typed by a user, prefer input("Name: "), which writes its prompt without a newline on its own.
file: sending output somewhere else
with open("report.txt", "w", encoding="utf-8") as out:
print("Total", 42, file=out)
import sys
print("something went wrong", file=sys.stderr)
Always pass encoding="utf-8" when you open the file. Windows gives files a local encoding by default, which is why the same script writes fine on one machine and crashes on another with a UnicodeEncodeError.
Formatting instead of gluing strings together
price, qty = 49.9, 3
print("Cost: ₹" + str(price * qty)) # works, brittle
print("Cost: ₹%.2f" % (price * qty)) # old style
print(f"Cost: ₹{price * qty:.2f}") # f-string - use this
f-strings evaluate expressions inside {}, so f"{price * qty:.2f}" prints 149.70: .2f is two decimal places, >8 pads to eight characters on the right, ,.0f adds thousands separators. Use repr() or f"{value!r}" when you need to see spaces and quotes.
Escapes you actually need
print("line one\nline two") # newline
print("col1\tcol2") # tab
print("backslash: \\\") # a literal backslash
print(r"C:\new\folder") # raw string: \n stays text, not a newline
The eight mistakes
| Mistake | What happens | Fix |
|---|---|---|
Python 2 style print "x" | SyntaxError: Missing parentheses | Always print("x"). Most old tutorials are Python 2. |
| Quoting inside quoting | The string ends early | Use the other quote type, or an f-string. |
print("Total: " + 42) | TypeError: can only concatenate str | print("Total:", 42) or f"Total: {42}". |
Trusting input() to be a number | ValueError on empty or lettered input | try / except ValueError around int(). |
print(list) when you wanted the items | Brackets and commas in the output | print(*list, sep=", "). |
| A stray comma creating a tuple | x = "a", "b" then print(x) shows ('a', 'b') | Remove the comma, or print the items: print(*x). |
| Backslashes inside a normal string | "\n" turns into a real newline mid-path | Use a raw string r"..." or forward slashes. |
| Progress lines that appear all at the end | Output is buffered | print(..., end="", flush=True). |
Two drills worth doing
- Print a multiplication table for 7 with every number padded to four characters, so the columns line up:
for n in range(1, 11): print(f"7 x {n} = {7 * n:>4}"). - Write three lines to a file, then print the same three lines to the screen, with the separator as
" | "and the ending as"!"on the last line only.
When a fixed example is not enough
These are the patterns that cover most real printing. When your version misbehaves, the useful move is to paste your exact line into a tutor and ask what it does differently — which is what Orbit's chat is for. It explains the one concept, sets a challenge on it, marks your answer, and answers the "why" you ask in the middle of a lesson without losing your place. It does this for about 40 lessons' worth of any skill you name, not only print.
Ask your own question — first 30 days free
New accounts get 30 days of the full Pro allowance with no card. After that the free plan still gives 15 tutor requests a day, so this kind of question keeps working without payment. See pricing.
More free guides
Python roadmap · Start with Python? · How long it takes · Hindi mein coding