What this tool does
The Python formatter rewrites Python code in the widely used Black style: consistent indentation, spaces around operators, double quotes, two blank lines between top-level definitions and long lines wrapped at sensible places.
Under the hood it runs the formatter from Ruff, the fast Python linter and formatter written in Rust, compiled to WebAssembly. Ruff’s output matches Black almost line for line, so code formatted here will not be reformatted again by your project’s pre-commit hook.
How to use it
- Paste Python code, or open a
.pyfile. - The formatted code appears on the right as you type.
- Adjust the options if your project uses a different style:
- Indent: 4 spaces is PEP 8 and the default; 2 spaces and tabs are available.
- Line width: 88 is Black’s default, 79 is PEP 8’s limit, and 100 or 120 suit wider screens.
- Quotes: double, single, or keep the quotes each string already uses.
- Respect trailing commas: keep collections that end with a comma expanded.
- Copy or download the result.
When the code cannot be parsed, the error message shows where the parser stopped, such as Expected `,`, found `=` on line 4, and you can jump to that line.
Example
The sample is a small module written carelessly: imports on one line with no spaces, a dataclass with a space before the colon, a method indented with two spaces and a one-line if statement. After formatting, every block uses four-space indentation, the if body moves onto its own line, operators get spaces, single quotes become double quotes and blank lines are added between the class and functions. The list that ended with a trailing comma is expanded with one dictionary per line.
What the formatter does not do
Formatting only changes layout. It does not sort imports, remove unused variables or rename anything; those are linter jobs (ruff check --fix handles them). Comments and docstrings are kept, and the formatter refuses to change code it cannot parse rather than guessing.
Tips
- Use the same line width as your project’s
pyproject.tomlso the result matches your CI. - Formatting a snippet before posting it in a question or review makes it much easier to read.
- If the code came from a notebook or a chat and has mixed tabs and spaces, the formatter normalises it in one step.
FAQ
› Is the output the same as Black?
Ruff's formatter is designed as a drop-in replacement for Black and matches its output on more than 99.9% of lines in large real-world projects. The default settings here (4-space indent, 88 columns, double quotes) are Black's defaults.
› What is the "magic trailing comma"?
If a list, call or definition ends with a trailing comma, Black and Ruff keep it expanded with one item per line even when it would fit on one line. Turn the switch off to collapse such collections whenever they fit.
› Which Python versions are supported?
The parser understands modern Python 3 syntax, including match statements, type parameter lists and f-strings with nested quotes. Python 2 code such as `print "x"` is a syntax error.
› Is my code uploaded?
No. The Ruff WebAssembly module is downloaded once and formatting runs in your browser.