October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Python Indentation Explained: Blocks, Spaces, and Common Errors

Python indentation determines which statements belong to a block. Learn the role of indentation levels, the four-space convention, and how to diagnose common errors.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python uses the whitespace at the start of a line to determine which statements belong together. Indentation therefore affects how code runs; it is not merely visual formatting. If you see an indentation error, check the block header, the lines beneath it, and whether each later line returns to an indentation level already in use.

How Python indentation groups statements

A line that begins farther to the right belongs to a nested block when the code’s structure calls for one. Lines at the same indentation level are siblings; returning to a previous level closes the deeper block.

As an Amazon Associate I earn from qualifying purchases.

if temperature > 25:
    print("It is warm")
    print("Consider some water")
print("Done")

The two indented print statements are inside the if body. The final statement returns to the outer level, so it is not part of that body. The colon introduces the block; the indentation marks its contents. See the Python 3.14.8 language reference on indentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What “expected an indented block” means

After a block-opening header such as if, for, while, or def, Python expects the block’s body to begin at a deeper indentation level. This code has no indented body:

if temperature > 25:
print("It is warm")

Indent the body so it sits under the header:

if temperature > 25:
    print("It is warm")

The error name and reported line can vary with the code and where Python detects the malformed structure. The underlying cause may be on the line shown or on a nearby line that established an unexpected level.

Four spaces is the convention, not a parser requirement

PEP 8, the Python style guide, recommends four spaces per indentation level and says, “Spaces are the preferred indentation method.” Four spaces make nested blocks easy to read, but the parser’s job is to recognize consistent indentation levels, not to require exactly four spaces. PEP 8 also allows tabs when maintaining code that already uses tabs. For new code, use spaces consistently. Read PEP 8’s indentation guidance.

Spaces, tabs, and TabError

Python expands tabs to tab stops when calculating indentation; each tab advances to the next multiple-of-eight position. As it reads code, it tracks the active indentation levels. An increase creates an INDENT token; a decrease must return to a level already established, producing one or more DEDENT tokens. A decrease to an arbitrary position is not a valid way to close a block.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If tabs and spaces are mixed so that the indentation is ambiguous, Python raises TabError. Code can look aligned in an editor while containing different whitespace characters, so visual appearance alone may not reveal the problem. Use one indentation style throughout the file, and replace suspicious leading whitespace consistently rather than adjusting lines by eye.

A practical way to find the mistake

  1. Locate the block header. Find the relevant line ending in a colon, such as an if, for, while, or def statement.
  2. Check the first body line. It should start at a deeper level than the header. If it does not, indent it and the other statements that belong in the body.
  3. Compare sibling statements. Statements meant to be part of the same block should line up exactly. Check that an else, elif, except, or finally aligns with its matching opener, and that the clause’s own body is indented beneath it.
  4. Check every dedent. When a line moves back to the left, align it with a level already used by an enclosing block. Do not choose an in-between position.
  5. Inspect nearby lines and whitespace. The reported line is a useful clue, but a preceding line may have created the unexpected level. If code was copied from elsewhere, replace its leading indentation consistently with spaces.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Learning Python beyond indentation

For a broader, hands-on introduction, No Starch Press lists Python Crash Course, 3rd Edition by Eric Matthes as a 552-page print book dated December 2022 (ISBN-13 9781718502703). Its publisher describes it as covering Python basics, exercises, projects, and troubleshooting. It is an optional learning resource, not required to understand indentation. See the publisher’s listing.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.