Python Formatter
Paste messy Python and get clean, consistently formatted code in the style used by Black and Ruff: normalised spacing and quotes, two blank lines around top-level functions and classes, and long lines split at brackets. Choose the line length, quote style and indent.
Last updated:
How to use the Python Formatter
- Paste your Python code into the left box, upload a .py file, or click Sample.
- Choose the options: line length (88 is the Black and Ruff default), quote style and indent width.
- Read the result on the right; it updates as you type and the status line shows how many lines changed.
- Fix syntax errors if the formatter reports one, such as an unclosed bracket or string.
- Copy or download the formatted code as a .py file.
What the formatter changes
| Before | After | Rule |
|---|---|---|
x=1+2*y | x = 1 + 2 * y | Spaces around assignment and binary operators |
f(a , b ,c) | f(a, b, c) | No space before a comma, one after |
def f(a = 1, *args, b : int=2) | def f(a=1, *args, b: int = 2) | No spaces around keyword defaults unless annotated |
x ** 2, f(x)**2 | x**2, f(x) ** 2 | Power operator hugs simple operands only |
ham[lower+offset:upper] | ham[lower + offset : upper] | Slice colons act like operators when an operand is complex |
'text', u'x', 0XFF, 1E5 | "text", "x", 0xFF, 1e5 | Preferred quotes and normalised prefixes and numbers |
if x: return y | if x: / return y on its own line | One statement per line |
x = 1 # note | x = 1 # note | Two spaces before an inline comment, one after # |
class A(): | class A: | Redundant parentheses removed |
How long lines are split
When a line is longer than the limit, the formatter first tries to split at the last opening bracket: the content goes on its own indented line, and the closing bracket returns to the original indentation. If the content still does not fit, it is split at the highest-priority delimiter: commas first (one item per line, with a trailing comma added), then comprehension clauses, conditional expressions, and/or, comparisons and arithmetic operators, with each operator starting a new line. Long right-hand sides of assignments and return statements are wrapped in parentheses so no backslashes are needed.
result = load_config("settings.env", defaults={"mode": "production", "debug": "false"}, strict=True)
# becomes (line length 88)
result = load_config(
"settings.env", defaults={"mode": "production", "debug": "false"}, strict=True
)Using the same style in your project
- Install a formatter with
pip install blackorpip install ruffand runblack .orruff format .in your project folder. - Set the line length once in
pyproject.toml:[tool.black] line-length = 100or[tool.ruff] line-length = 100. - Use
quote-style = "single"under[tool.ruff.format]if your team prefers single quotes; Black itself only supports double quotes (or leaving them unchanged with--skip-string-normalization). - Add the formatter to a pre-commit hook or your editor's format-on-save so code stays consistent without manual effort.
Frequently asked questions
What style does this Python formatter use?
It follows the Black code style, which Ruff's formatter also implements: 4-space indentation, double quotes, spaces around operators and after commas, two blank lines around top-level functions and classes, and lines wrapped at 88 characters by splitting inside brackets. Code formatted here matches what black or ruff format would produce in the vast majority of cases.
What is the difference between Black and Ruff format?
Black is the original opinionated Python formatter written in Python. Ruff is a much faster linter and formatter written in Rust whose formatter is designed as a drop-in replacement for Black and produces nearly identical output. This page runs Ruff compiled to WebAssembly when it can load, and otherwise a built-in JavaScript formatter that applies the same rules.
What is a magic trailing comma?
If a collection or argument list ends with a comma, as in foo(a, b,), Black and Ruff keep it exploded with one item per line even when it would fit on one line. This lets you choose the vertical layout for lists that change often. Untick the option to collapse such lists when they fit.
Does formatting change what my code does?
No. Formatters only change whitespace, line breaks, redundant parentheses, quote characters and the case of number prefixes, so the program's syntax tree stays the same. Black verifies this by comparing the AST before and after formatting, and the built-in engine here was tested the same way on the Python standard library.
Why is a long line not split?
Lines are split only at brackets, operators and commas. A single long string, a long comment or a long name cannot be broken without changing the code, so the formatter leaves it as it is. Wrap long strings yourself with implicit concatenation inside parentheses.
Is it PEP 8 compliant?
The output follows PEP 8 for indentation, whitespace and blank lines. Black's default line length is 88 instead of PEP 8's 79; pick 79 in the options if your project requires it.
Is my code uploaded?
No. Formatting runs entirely in your browser, either with the Ruff WebAssembly module or the built-in engine, so private code never leaves your device.
Are all the tools on FreeToolCabin tested for quality and accuracy?
Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.