Skip to main content
Programming Language Tools

Python Formatter

Format and re-indent Python source code consistently, entirely in your browser.

Paste your Python code below - this tool normalizes indentation consistently.

Since Python's indentation is semantically significant, this tool preserves your code's structure while only normalizing consistent spacing.

What Does This Tool Do?

It normalizes indentation in Python source code to a consistent width, making code that mixes tabs and spaces (or inconsistent indent levels from copy-pasting) readable and consistent again.

Why Use This Tool?

Since Python uses indentation to define code blocks, inconsistent spacing isn't just an aesthetic issue - mixed tabs/spaces can cause actual IndentationErrors. This tool normalizes it safely.

How to Use It

  1. Paste your Python code.
  2. Click Format.
  3. Copy the consistently-indented result.

Limitations

This tool never executes your Python code - it only re-indents based on the existing block structure it detects; it cannot fix genuinely broken/ambiguous indentation where the original structure can't be determined.

Example

Code that mixes 2-space and tab indentation, often from copy-pasting:

def greet(names):
  for n in names:
	  print("Hello", n)

is normalised to consistent 4-space levels:

def greet(names):
    for n in names:
        print("Hello", n)

Errors This Prevents

  • TabError: inconsistent use of tabs and spaces in indentation
  • IndentationError: unexpected indent
  • IndentationError: unindent does not match any outer indentation level

These are common when code is pasted from websites, chat apps or PDFs, which often convert indentation unpredictably.

For Projects: Black or Ruff

PEP 8 recommends 4 spaces per level. For full formatting - line length, quotes, spacing - use Black or Ruff:

pip install ruff
ruff format .

Add it as a pre-commit hook and style never needs discussing again. Configure it in pyproject.toml.

Related tools: Python Requirements Generator, JSON to Python and the Code Indentation Tool.

Frequently Asked Questions

No. It re-indents Python consistently in your browser. For project-wide formatting with a fixed style, use Black or Ruff format.

Python uses indentation to define blocks, so inconsistent spaces or mixing tabs and spaces changes meaning or causes IndentationError.

PEP 8 recommends 4 spaces per indentation level and no tabs.
Share this tool: