How to Debug With Print Statements as a Beginner
A student I coach spent most of a Tuesday evening convinced that Python was adding numbers wrong. Her function summed a list of scores and returned 0 every time.

A student I coach spent most of a Tuesday evening convinced that Python was adding numbers wrong. Her function summed a list of scores and returned 0 every time. She had re-read the eight lines maybe thirty times, which is the single least effective thing a person can do with a bug, and she was starting to believe the language was broken.
I asked her to add one line inside the loop: print("loop ran, score =", score). She ran it. Nothing printed. The loop had never run once, because the list she passed in was empty, because the function that built it returned early on a condition she had forgotten about. Thirty readings of the right eight lines had told her nothing, and one print told her everything in four seconds. That is not a lucky trick. It is the normal result, and it is why print debugging is still the first tool I teach, ahead of every clever alternative.
Reading code is guessing, printing is measuring
When you stare at code you are running it in your head, and your head runs the version you meant to write. That is the whole problem. The bug exists precisely because the code on the screen and the code in your mind have come apart, so the mental version will never show you the fault. It will confirm your intention, over and over, for as long as you are willing to sit there.
A print statement does something different in kind. It reports what actually happened at a specific moment, in the real run, with the real values. You stop asking what the code should do and start collecting facts about what it did. Beginners who make that switch get noticeably faster within a week or two, in my experience with contest students, far faster than those who try to get better at reading.
The habit also changes what a bug feels like. A bug you cannot see is frightening. A bug you are narrowing down with prints becomes a search, and a search has an end.
Three prints find most beginner bugs
You do not need variety here. Nearly everything I have debugged with a beginner came down to one of three questions, and each has a print that answers it.
The first is did this code run at all. print("reached the if branch") placed inside a branch, a loop body or a function. If it does not appear, your bug is not in that code, it is in whatever decides whether to go there. My student's empty list was this case, and it is the most common one by a wide margin. Beginners almost always assume the code ran and produced a wrong answer, when in fact it never executed.
The second is what is this variable right now. print("total =", total, type(total)). Including the type matters more than people expect, because a surprising share of beginner bugs are a string that looks like a number. "7" and 7 print almost identically, and "7" + "3" gives you "73" without raising any error at all. Printing the type exposes that in one line, where reading the code never will.
The third is how many times did this happen. Put a counter in and print it after the loop, or print the loop variable on every pass. Off by one errors, loops that run zero times and loops that run once too often all look identical in the output and completely different in the print trace.
Why I disagree about learning the debugger first
You will meet the advice soon enough, often phrased as a mild telling off: real programmers use a proper debugger, print debugging is a beginner crutch, learn breakpoints and step through properly. Experienced developers say it sincerely. For someone in their first six months I think it is the wrong order, and I say that having taught both.
A debugger gives you a paused program with a panel of every variable in scope, a call stack, and controls for step over, step into and continue. Every one of those concepts needs understanding before the tool helps. I have watched beginners sit in a paused program with the answer visible in the variables panel and not know which of the eleven names mattered. The tool showed everything, which is almost the same as showing nothing when you do not yet know what you are looking for.
A print forces the opposite. You have to decide, before running, which value you care about and at which point. That decision is the actual debugging skill, and printing is the only method that makes you practise it every time. The debugger is genuinely better once you have the skill, and it is far better for anything involving a deep call stack or a loop running ten thousand times. Learn it in your second year, not your second week.
There is one exception worth knowing. If your program crashes rather than misbehaves, the traceback already tells you where and roughly why, and printing is a slower route to the same place. Read the traceback first in that case, as our guide to reading a Python error message explains, and keep prints for the bugs that run cleanly and produce nonsense.
A routine that narrows a bug in about ten minutes
Scattering prints everywhere produces a wall of output you cannot read. This is the sequence I give students, and it works because it halves the suspect region each round rather than inspecting it line by line.
- Write down the wrong result and the right one before touching anything. "Returns 0, should return 240." Without this you will lose track of what you are even checking after three runs.
- Put one print at the start of the suspect function and one at the end, each showing the inputs and the return value. This tells you whether the problem is inside this function or in what gets handed to it. Roughly half the time you discover you are debugging the wrong function entirely.
- Pick the midpoint of the suspect code and print the key variable there. If the value is already wrong at the midpoint, the bug is above it. If it is still right, the bug is below. You have halved the search with one run.
- Repeat that split two or three times. Twenty lines of code reaches a single line in about four runs, which is why this beats working downwards from the top.
- Label every print with something searchable, like
print("A total:", total)andprint("B total:", total). Unlabelled numbers in a long output are unreadable, and you will not remember which line produced which figure. - When you find it, delete the prints in the same sitting. Leftover debug output in a project you hand in or share is the usual way this habit gets its bad reputation.
Step three is the part that feels unnatural and matters most. Beginners instinctively start at line one and work down, which means the fastest bugs to find are at the top and the slow ones take the whole evening. Splitting in the middle makes every bug cost about the same.
Small habits that make the output usable
Print the variable name with the value, always. print(total) gives you a bare number that means nothing after the fifth run. In Python 3.8 and later there is a shortcut for this: print(f"{total=}") prints total=240 without typing the name twice, which removes the main excuse for lazy prints.
Print a whole dictionary or list rather than one key when you are unsure, since the surprise is usually something you were not checking. For anything nested, import pprint then pprint.pprint(data) lays it out on multiple lines and saves you squinting at a single long row.
Keep a marker you can search for, such as starting every temporary print with XX. When the bug is fixed, searching the file for XX finds every line to remove in a few seconds. I have seen more than one student hunt through a file for twenty minutes looking for one forgotten print.
One honest limit: prints change timing, so for code involving threads or network calls a bug can move or vanish when you add one. That is rare in a first year and genuinely confusing when it happens, so if a bug disappears every time you try to observe it, that is the point to ask someone rather than keep going. Our notes on asking a coding question that gets a real answer cover what to include, and the rest of first steps has the groundwork this sits on.
FAQ
Is print debugging bad practice?
No, and plenty of professionals still do it daily. What is bad practice is leaving the prints in shared code, or printing without labels so the output cannot be read. In a long running program the grown up version of the same idea is the logging module, which is print with levels and timestamps.
Where should I put the first print?
At the boundary of the code you suspect, showing the inputs going in and the value coming out. That one run tells you whether to look inside the function or outside it, which is the most valuable single fact you can get at the start.
My print does not appear at all. What does that mean?
Usually that the line never ran, which is a real finding rather than a failure. Check the condition or loop that should have led there. Occasionally the program crashed earlier than you thought, so check whether a traceback appeared above your output.
How many prints is too many?
If you cannot read the output, you have too many. Four or five in one run is usually workable, and more than that means you should narrow the region first using the midpoint split rather than printing everything at once.
More in First Steps
First Steps
How to Escape Tutorial Hell and Build Something Real
One of our club members turned up last autumn having watched roughly sixty hours of Python video across four months.
First Steps
How to Ask a Coding Question That Gets a Real Answer
A student in my Thursday group spent eleven days stuck on the same program.
First Steps
How to Use AI to Learn Coding Without Skipping the Thinking
You ask an AI chatbot to write a Python program that counts words in a file, it hands you 14 working lines, and you feel great...