Python Formatter
Format and re-indent Python source code consistently, entirely in your browser.
Paste your Python code below - this tool normalizes indentation consistently.
More Programming Language Tools
View all 23Since 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
- Paste your Python code.
- Click Format.
- 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 indentationIndentationError: unexpected indentIndentationError: 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
Explore More Tools
Keep going with related categories and our most used tools.