If you’re like me, you try things first in your code and fix the bugs as they come. One frequent bug in Python is the
IndentationError: unexpected indent. So, what does this error message mean?
IndentationError: unexpected indent arises if you use inconsistent indentation of tabs or whitespaces for indented code blocks such as the
if block and the
for loop. For example, Python will throw an indentation error, if you use a
for loop with three whitespace characters indentation for the first line, and one tab character indentation of the second line of the loop body. To fix the error, use the same number of empty whitespaces for all indented code blocks.
Let’s have a look at an example where this error arises:
for i in range(10): print(i) print('--')
The first line in the loop body uses two whitespaces as indentation level. The second line in the loop body uses three whitespace characters as indentation level. Thus, the indentation blocks are different for different lines of the same block. However, Python expects that all indented lines have structurally the same indentation.
How to Fix Python’s Indentation Error?
To fix the error, simply use the same number of whitespaces for each line of code:
for i in range(10): print(i) print('--')
The general recommendation is to use four single whitespace characters
' ' for each indentation level. If you have nested indentation levels, this means that the second indentation level has 4+4=8 single whitespace characters:
for i in range(10): for j in range(10): print(i, j)
Mixing Tabs and Whitespace Characters Often Causes The Error
A common problem is also that the indentation seems to be consistent—while it really isn’t. The following code has one tab character in the first line and four empty whitespaces in the second line of the indented code block. They look the same but Python still throws the indentation error.
On first sight the indentation looks the same. However, if you go over the whitespaces before
print(i), you see that it consists only of a single tabular character while the whitespaces before the
print(j) statement consists of a number of empty spaces
Try It Yourself: Before I tell you what to do about it, try to fix the code yourself in our interactive Python shell:
Exercise: Fix the code in the interactive code shell to get rid of the error message.
Do you want to develop the skills of a well-rounded Python professional—while getting paid in the process? Become a Python freelancer and order your book Leaving the Rat Race with Python on Amazon (Kindle/Print)!
How to Fix The Indentation Error for All Times?
The source of the error is often the misuse of tabs and whitespace characters. In many code editors, you can set the tab character to a fixed number of whitespace characters. This way, you essentially never use the tabular character itself. For example, if you have the sublime text editor, the following quick tutorial will ensure that you never run in this error ever again:
Sublime Textto use tabs for indentation:
Convert Indentation to Tabs
- Uncheck option
Indent Using Spacesin the same sub-menu above.
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.