Learning LibraryCore Coding LibraryTeens

print() in Python: see what your code computed

A calculation nobody can see is useless. print() is how a program reports its results, and it's the first tool you reach for when a number looks wrong.

The big idea

print() writes its arguments to the console, separated by spaces and followed by a new line.

See it in code

1The basics

A bare number tells you nothing an hour later. Pass a label and a value, separated by a comma, and the output explains itself:

python
price = 38
cost = 14
print("Profit per tee:", price - cost)
Run it. A label, then a value:
Profit per tee: 24

The comma separates two arguments, and print() puts a space between them. The expression price - cost is evaluated before anything is shown.

2A step further

print() accepts any number of arguments, of any types. Two optional settings, sep and end, control what goes between them and what comes after:

python
tees = 1000
box_size = 24
print("Full boxes:", tees // box_size, "| Left over:", tees % box_size)
print("TEE", "SAGE", "M", sep="-")
print("Packing...", end=" ")
print("done")
Run it. Default spacing, then custom:
Full boxes: 41 | Left over: 16
TEE-SAGE-M
Packing... done

sep="-" replaced the default space, building a SKU from three pieces. end=" " replaced the default new line, so the next print continued on the same line.

3At Evergreen

Printing is also how you verify a result. Compute season profit two independent ways and print both; if they disagree, one of your formulas is wrong:

python
price = 38
cost = 14
tees_sold = 650

season_profit = (price - cost) * tees_sold
revenue = price * tees_sold
cost_of_goods = cost * tees_sold

print("Season profit:", season_profit)
print("Revenue minus costs:", revenue - cost_of_goods)
Run it. Two routes to the same number:
Season profit: 15600
Revenue minus costs: 15600

Both lines print 15600. Agreement between two methods isn't proof, but disagreement is a guaranteed bug, and you find it in seconds.

The same idea, everywhere

Print-checking works in every domain. Print a DNA strand's length before slicing it, a moving average beside the prices it came from, or a variable just before the line that crashes.

Try it yourself

Add print("Margin:", (price - cost) / price) and look at the long decimal. Then format it properly with an f-string: print(f"Margin: {(price - cost) / price:.1%}").

The common mistake

Treating print() as if it returned the value. It displays its arguments and returns None, so total = print(revenue) stores None. To hand a value to other code, a function must return it.

What it unlocks

For control over decimals, commas, and percentages, move to f-strings. For the difference between showing a value and handing one back, see return values.

Want the simpler version? Read the Kids version →