Python Formatter
Normalise Python indentation, strip trailing whitespace, tidy spacing after commas and colons, and collapse runs of blank lines. A quick paste-and-tidy for code that arrived badly indented — not a replacement for Black.
String literals and comments are protected before any spacing change, so their contents are never rewritten. This tool does not reflow long lines or reorder imports — see below.
How it works
What this tool does
It detects the indentation step your file actually uses, then re-emits every line at a consistent width — converting tabs to spaces or vice versa as you choose. It also strips trailing whitespace, collapses three or more blank lines to two, and optionally normalises spacing after commas and colons.
Before any spacing change, string literals (including triple-quoted strings) and comments are extracted and set aside, then restored. Without that step a spacing rule would corrupt the contents of your strings.
What it deliberately does not do
It is not Black, autopep8 or Ruff, and it does not pretend to be. It does not reflow long lines, add magic trailing commas, reorder or group imports, normalise quote characters, or restructure expressions — all of which require parsing Python properly rather than processing it line by line.
For real formatting, run black . or ruff format . in your project. Use this page for the narrower job it does well: code pasted out of a PDF, an email, a Jupyter cell or a chat window, where the indentation has been mangled and you just need it consistent again.
Why indentation matters more in Python
In most languages indentation is cosmetic. In Python it is the block structure, so a mangled indent is a syntax error or — worse — silently valid code that does something different. A line dropped out of a loop body still parses; it just runs once instead of many times.
PEP 8 specifies 4 spaces per level and spaces rather than tabs. Mixing tabs and spaces is a TabError in Python 3, which is why normalising to one or the other is the first thing to do with pasted code.
What people use it for
- Fixing code pasted from a PDF, email or chat where indentation was lost or doubled.
- Converting tabs to spaces to resolve a
TabError. - Normalising a file that two editors with different settings have touched.
- Cleaning up a Jupyter cell before moving it into a module.
- Tidying a snippet before pasting it into documentation or an issue.
Nothing is uploaded. This tool runs entirely in JavaScript in your browser. Your input is never sent to a server, never logged and never stored, and no account is required. Once the page has loaded you can go offline and it still works — open your browser's network tab and watch if you'd like to confirm it.
Frequently asked questions
Is this the same as Black?
No. Black parses Python and reformats it completely, including reflowing long lines and adding trailing commas. This tool works line by line: it normalises indentation, whitespace and simple spacing. For real formatting run black . or ruff format . in your project.
Will it break my strings?
No. String literals, including triple-quoted ones, and comments are extracted before any spacing change and restored afterwards, so their contents are never modified.
Does it reorder my imports?
No. Import sorting needs a parser and project context. Use isort or Ruff for that.
Why does Python care so much about indentation?
Because indentation is the block structure, not decoration. Mangled indentation is either a syntax error or, worse, valid code that behaves differently. Mixing tabs and spaces raises TabError in Python 3.
How many spaces should I use?
Four, per PEP 8, using spaces rather than tabs. That is the default here.
Tools that run where your code is
This page does its work in your browser because sending your data to a server to process it is a bad default. Catalase applies the same idea to AI coding: a free, open-source code editor that runs language models on your own machine through Ollama, with no account and no telemetry. Cloud models are there when you choose one, not by default.
Related tools
- SQL formatter — Break long queries onto readable lines.
- Diff checker — Compare two versions of a file.
- Character counter — Characters, words, lines and UTF-8 bytes.