Python Formatter

Clean up Python whitespace, tabs, and blank lines without touching logic.

About this tool

Clean up messy Python source: convert tabs to spaces, strip trailing whitespace, collapse runs of blank lines, and normalize line endings — the whitespace cleanup a linter would flag, without touching your code's logic or structure.

This does not re-indent or reflow your code the way a full formatter like Black would. Python's indentation is semantically meaningful, so guessing new indentation from scratch risks silently changing what your code does — this tool only ever normalizes whitespace that's already consistent, never restructures it.

What each option actually does, mechanically:

OptionWhat it doesPEP 8 rule
Convert tabs to spacesReplaces every literal tab character with exactly 4 spaces, 1:1 — it doesn't inspect or resize indentation that's already made of spacesSpaces are the preferred indentation method
Strip trailing whitespaceRemoves spaces and tabs at the end of every line, including inside blank linesNo trailing whitespace
Collapse extra blank linesRuns of 3+ consecutive blank lines become 2; one or two blank lines are left as-isSurround top-level defs/classes with 2 blank lines

Why it won't fix 2-space vs. 3-space vs. 4-space indentation. "Convert tabs to spaces" only touches actual tab characters (\t) — if your file's indentation is already made of spaces, whether 2, 3, or 4 per level, and inconsistently at that, this tool leaves it completely untouched. Detecting and normalizing an indent width means understanding which lines belong to which block, which means parsing Python's grammar — exactly what a regex-based, browser-side tool deliberately doesn't attempt, because a wrong guess could silently change which lines run inside a loop or function versus after it.

The error this tool actually helps with. Python 3 refuses to run a file that mixes tabs and spaces in a way it can't unambiguously resolve, raising TabError: inconsistent use of tabs and spaces in indentation. Converting every tab to 4 spaces is one of the most common fixes, since it removes the ambiguity between "one tab" and "some number of spaces" entirely — but it's a fix for tab/space mixing specifically, not for a file where every line already uses spaces but at inconsistent widths.

For actual re-indenting, reach for a real formatter that parses your code into a syntax tree first — Black, autopep8, or ruff format, installed with pip (see the requirements.txt generator if you're pinning it in a project). This tool is the safe first pass before that, or the whole fix when tabs (not indent width) were the only problem. The result is syntax-highlighted by default — toggle "Raw" if you need the plain text instead, for pasting somewhere that doesn't render HTML. For other languages, see JS formatter, HTML formatter, and JSON formatter.

Example

Input (tab-indented, blank-line run)
def greet(name):
	msg = "hi " + name



	return msg
Cleaned
def greet(name):
    msg = "hi " + name

    return msg

Tabs become 4 spaces and the run of blank lines collapses to one — the indentation structure is left exactly as it was. Trailing spaces and inconsistent line endings are cleaned up the same way.

Frequently asked questions

Why doesn't this re-indent my code like Black or autopep8?
Those tools parse Python into a syntax tree to safely rewrite indentation and line breaks. A browser-side regex/text tool can't safely do that for an indentation-sensitive language — get those tools via pip for full reformatting; this one focuses on the whitespace cleanup that's always safe.
Will "Convert tabs to spaces" break my indentation?
No — each tab character is replaced with 4 spaces, preserving the relative indentation depth exactly, which is the standard PEP 8 convention.
What counts as an "extra" blank line?
Runs of 3 or more consecutive blank lines are collapsed down to 2 (the PEP 8 convention for separating top-level definitions); single or double blank lines are left untouched.
Does this fix inconsistent indent widths, like a mix of 2-space and 4-space indentation?
No. "Convert tabs to spaces" only replaces literal tab characters — it never inspects or resizes indentation that's already made of spaces, at any width. Normalizing indent width safely requires parsing which lines belong to which block, which is what a real formatter like Black does; a text-only tool can't do that without risking a wrong guess that changes your code's structure.
What causes Python's "inconsistent use of tabs and spaces" error?
Python 3 raises TabError when a file mixes tabs and spaces in a way it can't unambiguously resolve into indentation levels. Converting every tab to spaces (this tool's main job) removes that ambiguity and is one of the most common fixes — though it won't help if the error is instead caused by inconsistent space-only indent widths.
Could stripping trailing whitespace break a string that intentionally ends in spaces?
Yes, in principle — this tool works on raw text lines, not a parsed syntax tree, so it can't tell a trailing space in your code from one inside a multi-line string literal's actual content. That's rare, but if your file deliberately preserves trailing spaces inside a string, uncheck "Strip trailing whitespace" first.
Does this run Black or autopep8 in my browser?
No — neither is a small, portable JavaScript library, and both need a real Python parser to work safely. This tool is a lightweight, always-safe whitespace pass; install Black, autopep8, or ruff via pip for full reformatting.