FREE GUIDE

Python print(): everything it does, and the eight mistakes that break it

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)

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

Two drills worth doing

  1. 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}").
  2. 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.

More free guides

Python roadmap · Start with Python? · How long it takes · Hindi mein coding