PyGuide

Learn Python with practical tutorials and code examples

How to Fix Python Indentation Error When Mixing Tabs and Spaces

Python indentation errors when mixing tabs and spaces are one of the most common syntax issues beginners encounter. This guide provides immediate solutions to fix these errors and prevent them from happening again.

What Causes This Error? #

Python's strict indentation rules require consistent use of either tabs OR spaces, but not both. When you mix them, Python throws an IndentationError:

# This code will cause an error
def my_function():
    print("First line")  # 4 spaces
    print("Second line")  # 1 tab (appears as 4 spaces but different character)

Error message: IndentationError: inconsistent use of tabs and spaces in indentation

Quick Fix Solutions #

Most Python developers use 4 spaces for indentation. Here's how to fix it:

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Solution 2: Use Your Editor's "Show Whitespace" Feature #

Enable whitespace visualization in your code editor:

  • VS Code: View → Render Whitespace
  • PyCharm: View → Active Editor → Show Whitespaces
  • Sublime Text: View → Show Console, then type view.settings().set("draw_white_space", "all")

Solution 3: Automatic Fix with Editor Features #

VS Code:

  1. Open Command Palette (Ctrl+Shift+P)
  2. Type "Convert Indentation to Spaces"
  3. Select and run the command

PyCharm:

  1. Code → Reformat Code
  2. Or use Ctrl+Alt+L (Windows/Linux) / Cmd+Opt+L (Mac)

Common Scenarios and Fixes #

Copying Code from Different Sources #

When you copy code from websites or different editors:

# Before (mixed indentation - will cause error):
def process_data():
    data = [1, 2, 3]  # 4 spaces
    for item in data:  # Tab character
        print(item)    # Tab + 4 spaces

# After (consistent spaces):
def process_data():
    data = [1, 2, 3]  # 4 spaces
    for item in data:  # 4 spaces
        print(item)    # 8 spaces

Nested Code Blocks #

🐍 Try it yourself

Output:
Click "Run Code" to see the output

Prevention Tips #

1. Configure Your Editor #

Set your editor to:

  • Show whitespace characters
  • Use spaces instead of tabs
  • Set tab width to 4 spaces
  • Enable auto-indentation

2. Use Python's -tt Flag #

Run Python with strict tab checking:

python -tt your_script.py

This will catch mixed indentation errors more aggressively.

3. Use Code Formatters #

Install and use code formatters like:

  • Black: pip install black, then black your_file.py
  • autopep8: pip install autopep8, then autopep8 --in-place your_file.py

Troubleshooting Persistent Issues #

If you still get errors after fixing visible indentation:

  1. Check for invisible characters: Some text editors or copy-paste operations introduce non-breaking spaces
  2. Recreate the file: Copy your code to a new file and retype the indentation
  3. Use a hex editor: Check for unusual whitespace characters

Summary #

To fix Python indentation errors when mixing tabs and spaces:

  • Immediate fix: Convert all indentation to 4 spaces
  • Use editor tools: Enable whitespace visualization and auto-formatting
  • Prevention: Configure your editor properly and use code formatters
  • Best practice: Always use 4 spaces for indentation (PEP 8 standard)

Remember: Consistency is key in Python indentation. Pick one method (preferably 4 spaces) and stick with it throughout your entire codebase.