Python TabError Exception
Example
A TabError occurs if tabs and spaces are mixed in the indentation:
if True:
print("Indented with 2 spaces")
print("Indented with a tab")
Try it Yourself »
Definition and Usage
The TabError exception occurs when the indentation of a code block mixes tabs and spaces in a way that is not consistent.
In the example, the first line in the if block is indented with 2 spaces, and the second line with a tab.
TabError is a kind of IndentationError.
Tip: Use only spaces for indentation. Most code editors can insert spaces when you press the Tab key.
Exception Handling
Example
Handling the TabError in a try...except statement:
code = "if True:\n x = 1\n\ty = 2"
try:
exec(code)
except TabError:
print("Do not mix tabs and spaces")
Try it Yourself »
Related Pages
Read more about handling errors in our Python Try Except Tutorial.