Python Formatter
Paste Python and get it back formatted the way Black would do it: tidy spacing, consistent quotes, one statement per line and long lines wrapped sensibly. The formatter is the Ruff engine compiled to WebAssembly, so everything runs in your browser.
- Runs in your browser
- No sign-up
- Free to use
How to use Python Formatter
- Paste Python code or open a .py file.
- Choose the line length, indentation and quote style.
- Select Format Python.
- Copy the result, and review any code hints listed below the editor.
Python Formatter features
Black-compatible output
Produces the widely adopted Black code style using the Ruff formatter.
PEP 8 spacing
Spaces around operators and after commas, two blank lines between top-level definitions, compound statements split onto separate lines.
Smart line wrapping
Long calls, lists and dictionaries are broken one element per line with a trailing comma.
Syntax errors located
Invalid code is not guessed at; the error and its line are reported.
Code hints
Flags unused imports and variables, undefined names, mutable default arguments and comparisons with None using ==.
Fully local
The code is never uploaded. The formatter itself is downloaded once and then runs offline.
When to use Python Formatter
- Cleaning up a script before sharing it or asking for help.
- Formatting a snippet when no Python tooling is installed.
- Bringing code in line with a project that uses Black.
- Spotting unused imports and similar slips in a quick review.
Python Formatter FAQ
Which style does the formatter apply?
The Black style, which is a strict interpretation of PEP 8: four-space indentation, double quotes, 88-character lines, trailing commas in multi-line constructs. The engine is Ruff's formatter, designed to produce output practically identical to Black.
Why 88 characters and not 79?
PEP 8 recommends 79. Black chose 88 because it yields noticeably fewer wrapped lines while still fitting two files side by side on a screen. Both are offered, along with 100 and 120 for teams that prefer longer lines.
Does formatting change what my program does?
No. The formatter only changes layout: whitespace, line breaks, parentheses used for wrapping and the quote characters of strings where the content allows. The syntax tree of the code is the same before and after.
What are the code hints?
A small set of checks for things that are very likely mistakes: imports and variables that are never used, names that are not defined, a mutable default argument such as rows=[], bare except clauses and comparisons like == None. They are suggestions and do not block formatting.
Which Python versions are supported?
Current Python 3 syntax, including type hints, f-strings, the walrus operator, structural pattern matching and exception groups. Python 2 code, such as print without parentheses, is reported as a syntax error.
Why is there a short wait the first time?
The formatter is a WebAssembly module of about ten megabytes, downloaded when you first format and then cached by the browser. Later runs start immediately.
One style, no arguments
Python has an official style guide, PEP 8, which most Python code roughly follows. Roughly is the problem. PEP 8 leaves many choices open: where to break a long call, single or double quotes, how to indent continuation lines. Teams used to spend review time on those questions. Black settled them by taking the choices away. It reformats entire files into one canonical style, with almost no options, and the community largely adopted it for exactly that reason.
Because the formatter rebuilds the layout from the syntax tree, its output depends only on the code, not on how it was typed. The same program written by two people comes out identical. Diffs become small, since a change to one argument touches one line. The “magic trailing comma” gives authors a simple control: a collection written with a trailing comma stays one item per line, and without it, it is joined onto one line if it fits.
Ruff is a newer toolchain, written in Rust, that reimplements Black's formatting along with hundreds of lint rules at very high speed. Compiled to WebAssembly, the same engine runs in a browser tab, which is what this tool uses. The hints shown under the editor come from Ruff's linter, limited here to rules that point at probable bugs and leaving matters of taste aside.
For regular work, install the formatter in your editor and run it on save, and add it to continuous integration so that unformatted code cannot be merged. A browser tool is the convenient option for the moments in between: a snippet from a colleague, a script on a machine without your setup, a quick check of how a construct will be laid out.