Learning LibraryCore Coding LibraryTeens

print() in Python: your first debugging tool

print() looks basic, but it is the tool you will reach for most: it is how you see what your code is actually doing when something is off.

The big idea

print() writes its arguments to the screen, joined by spaces and followed by a new line — both of which you can change.

See it in code

1The basics

print() writes whatever you pass it to the screen, then moves to a new line:

python
print("Backtest complete.")
Run it — one message, one line:
Backtest complete.

One call, one line. That new-line-after is the default you'll bend in a moment.

2A step further

print() takes several values at once and joins them with spaces — labels and numbers in a single call:

python
trades = 12
profit = 340
print("Trades:", trades, "Profit:", profit)
Run it — four arguments, one line, spaces added for you:
Trades: 12 Profit: 340

Four arguments, one line, spaces auto-inserted between each. That's the readable summary line every log is built from.

3In our world

Now the full summary. The end= option controls what comes after a line — here we end one print with a space instead of a new line, so two prints share a row:

python
trades = 12
profit = 340

print("Backtest complete.")
print("Trades:", trades, "| Profit:", profit)
print("Status:", end=" ")
print("PASS")
Run it — note how end=" " keeps the last two prints on one line:
Backtest complete.
Trades: 12 | Profit: 340
Status: PASS

By default every print() ends with a new line; end=" " swapped that for a space, so Status: and PASS share a line. There is also a sep= option to change the space Python puts between arguments.

The same idea, everywhere

The same call logs a sprite's position each frame, a DNA base count, or the value of a variable you suspect is wrong. Sprinkling print() to watch values change — print-debugging — is the fastest way to find where a program's reality diverges from your mental model.

Try it yourself

Print Trades:, trades, Profit:, and profit in a single call, then add sep=" | " and watch the separators appear. Reach for a stray print() next time a loop misbehaves.

The common mistake

Assuming print() hands back a value you can use. It does not — it returns None. Writing x = print("hi") stores None in x, not the text. print() shows a value; it does not give one back.

What it unlocks

Clear output pairs with f-strings for formatted lines, and print-debugging sits at the heart of reading errors and debugging.

Want the simpler version? Read the Kids version →