Overview
Introduction
Python code pasted from different editors sometimes mixes tabs and spaces, or carries trailing whitespace and inconsistent blank-line spacing.
This tool cleans up that whitespace without touching indentation depth, entirely in your browser.
What Is Python Formatter?
A whitespace normalizer, not a full Python formatter: it converts leading tabs to 4 spaces (consistently, per line), trims trailing whitespace, and collapses runs of blank lines to at most one.
It deliberately never re-derives indentation from code structure, since in Python that structure is semantically significant.
How Python Formatter Works
Each line's leading tabs are expanded to spaces and trailing whitespace is stripped.
Consecutive blank lines are collapsed to a single blank line, and a single trailing newline is ensured.
When To Use Python Formatter
Use it when a Python snippet has mixed tabs and spaces, or excess blank lines, after being copied from elsewhere.
It's a safe cleanup step precisely because it can't change existing indentation depth.
Often used alongside Ruby Formatter and TOML Formatter.
Features
Advantages
- Runs entirely client-side with no Python interpreter required.
- Never risks changing program behavior, since it doesn't touch relative indentation.
- Cleans up trailing whitespace and excess blank lines in a single pass, with no configuration needed.
Limitations
- Doesn't reformat line length, quote style, or spacing around operators the way Black does.
- If tabs and spaces were already inconsistently mixed within the same line, this won't reliably fix it.
Examples
Best Practices & Notes
Best Practices
- For real formatting (line length, string quote style), run Black or a similar tool locally; use this for a quick whitespace cleanup first.
- Run this before diffing two versions of a file, so trailing-whitespace and blank-line noise doesn't obscure the real changes.
Developer Notes
Implemented as a pure string transform: per-line trailing-whitespace trim, tab-to-space expansion on leading whitespace only, and blank-line run collapsing. No AST parsing is involved.
Python Formatter Use Cases
- Cleaning up a Python snippet copied from a PDF, slide, or chat tool
- Fixing mixed tabs/spaces before pasting into an editor that enforces one or the other
Common Mistakes
- Expecting this to fix line length, string quoting, or import ordering; those require a real formatter like Black or isort.
- Assuming it fixes indentation depth itself; a line indented with the genuinely wrong number of spaces passes through unchanged.
Tips
- If indentation still looks wrong after normalizing, check the original source for genuinely inconsistent indentation depth, which this tool intentionally won't alter.
- Run the output through a linter like flake8 or ruff afterward to confirm it's still valid Python, since this pass doesn't validate syntax.