TabError

TabError is a built-in exception that occurs when you mix tabs and spaces in your code indentation.

It’s a subclass of IndentationError, which is a broader category for indentation issues and itself a subclass of SyntaxError.

To avoid this error, stick to four spaces for indentation, as per PEP 8 guidelines.

TabError Occurs When

  • Mixing tabs and spaces for indentation in a single code block
  • Copying and pasting code from different sources with inconsistent indentation

TabError Example

An example of when the exception appears:

Python mix.py
def greet(name):
    print(f"Hello, {name}!")  # 4 spaces
    print("Welcome to Real Python!")  # 1 tab

greet("Ada")

When you run a script that mixes spaces and tabs for indentation in the same code block, then you’ll encounter a TabError:

Python Traceback
  File "/path/to/mix.py", line 3
        print("Welcome to Real Python!")  # 1 tab
    ^
TabError: inconsistent use of tabs and spaces in indentation

You typically don’t raise or handle a TabError manually in your code. Python automatically raises this exception when it detects inconsistent use of tabs and spaces.

TabError How to Fix It

To fix a TabError, you need to ensure that your code uses consistent indentation. Here’s the corrected version of the previous example:

Python greeter.py
def greet(name):
    print(f"Hello, {name}!")  # 4 spaces
    print("Welcome to Real Python!")  # 4 spaces

greet("Ada")

The Python REPL and many integrated development environments (IDEs) automatically change tabs to spaces to avoid this mismatch.

Tutorial

Python's Built-in Exceptions: A Walkthrough With Examples

In this tutorial, you'll get to know some of the most commonly used built-in exceptions in Python. You'll learn when these exceptions can appear in your code and how to handle them. Finally, you'll learn how to raise some of these exceptions in your code.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated March 26, 2025 • Reviewed by Martin Breuss