Python Beautifier
Format Python code in Black style with Ruff.
Python Beautifier – Black-Style Formatting Online
The Python beautifier formats Python code with the Ruff formatter, a modern, extremely fast formatter that produces output compatible with Black, the de-facto standard Python style. It runs in your browser as WebAssembly, so your code is never uploaded.
What the formatter does
- Normalizes indentation to 4 spaces (or your choice).
- Adds standard spacing around operators and after commas, and removes spaces inside brackets.
- Wraps long function calls, definitions, and collections that exceed the line length, using the "magic trailing comma" to keep exploded lists expanded.
- Normalizes string quotes to double quotes (or single, or keeps them).
- Inserts two blank lines between top-level functions and classes, as PEP 8 recommends.
- Splits compound statements such as
with open(path) as f: data = f.read()onto separate lines.
Options
- Indentation – 4 spaces (PEP 8), 2 spaces, or tabs.
- Line length – 79 for strict PEP 8, 88 (the Black default), 100, or 120.
- Quotes – double, single, or preserve the existing quotes.
Syntax errors
Unlike a simple re-indenter, the formatter parses your code first. If it is not valid Python — a missing colon after def, unbalanced parentheses, or mixed tabs and spaces — you see an error describing where parsing failed instead of broken output.
Why Black style?
Black-style formatting is "uncompromising": there are very few options, so every project looks the same and code reviews focus on behaviour. Ruff's formatter follows Black's rules more than 99.9% of the time on real-world code, so you can format snippets here and they will match your project's formatter.
Related tools
Validate JSON data your script reads with the JSON Validator, format SQL strings with the SQL Beautifier, or tidy YAML configs with the YAML Beautifier.
Frequently Asked Questions
Is this the same as running Black?
It uses Ruff's formatter, which is designed as a drop-in replacement for Black and produces identical output for almost all code. Tiny differences exist in rare edge cases.
Which Python versions are supported?
Python 3 syntax up to the latest releases, including match statements, the walrus operator, type parameter syntax, and f-strings with nested quotes.
Can it fix my indentation if the code is broken?
Python's structure depends on indentation, so code must parse first. If indentation is inconsistent, the formatter reports a syntax error; fix the reported line and format again.
Does it sort imports or remove unused ones?
No. Formatting never changes what the code does. Use Ruff's linter or isort for import sorting.