# Python Formatter

URL: https://discuvr.org/python-formatter/
Last updated: 2026-11-07
MCP tool: `format_python`

Paste Python and get it formatted by Black with your line length and options. Syntax errors get the exact line. Your code is never run and never stored.

This runs Black - the formatter most Python projects already agree on - over whatever you paste, with the line length and quote handling you choose. If the file does not parse it says which line, and if it is already formatted it says that too rather than pretending to have done something.

## What it does

- Black formatting at 79, 88, 100 or 120 columns.
- "Check only" mode: find out whether a file would change without changing it.
- Keep your own quote style, or let Black normalise it.
- Honour or ignore the magic trailing comma that keeps a collection exploded.
- Target a specific Python version when the syntax is version-sensitive.
- Syntax errors report the line, the column and the code around it.
- Reports how many lines were added and removed, plus the file's shape.

## How it works

1. Paste the code, or a file's worth of it.
2. Choose a line length and any options you care about.
3. Press Format, then copy or download the result.

## Your code is never executed

Formatting Python does not require running it. The code is parsed into a syntax tree and rewritten from that tree, entirely in memory - it is never written to a file, never imported and never executed. A formatter that ran what you pasted would be a remote shell with a text box.

## Why Black, and not an opinion of our own

Black is an AST-preserving transform: the code that comes out is the same program as the code that went in. That property is the whole reason a formatter can be trusted, and it is why no language model is involved anywhere in this tool. A model asked to tidy code will occasionally rewrite a line while it is there, and a silent change to working code is the one failure a formatter cannot afford.

## Check only

The same question CI asks: would this file change? Check mode returns your original code untouched and reports whether Black would have reformatted it, and by how many lines. It is the quick way to find out why a pipeline is failing on a file you cannot reproduce locally.

## Magic trailing comma and fmt: off, worked examples

A comma after the last item tells Black to keep a call or collection one item per line: result = compute(alpha, beta,) comes back as four lines, with alpha, and beta, each on its own line. Tick Ignore magic trailing comma and the same input becomes result = compute(alpha, beta) whenever it fits the line length. To protect a hand-aligned block such as a matrix, put # fmt: off on the line above it and # fmt: on below it, and Black leaves everything in between as you wrote it; # fmt: skip at the end of a line protects that one line. These are Black's own comments, so they keep working when your project's CI runs Black.

## Questions

**Is my code uploaded or stored?**

It is sent to the server, formatted in memory and returned in the same response. Nothing is written to disk, cached or logged, and the usage record keeps only counts - size, options, how many lines changed - never the code.

**Does formatting change what my code does?**

No. Black changes layout, not behaviour, but it does more than move spaces. It removes redundant parentheses (x = (1) becomes x = 1), lower-cases number prefixes and exponents (0XABCDEF becomes 0xABCDEF, 1E5 becomes 1e5), adds a trailing comma when it splits a call over several lines, collapses runs of blank lines and trims the spaces inside a docstring (""" Doc. """ becomes """Doc."""). That last change alters the text of __doc__, which matters only if your code reads its own docstrings.

**Why did it not reformat my file at all?**

Because it was already formatted at the line length you chose. The tool says "already formatted" rather than quietly returning a copy.

**Does it sort my imports?**

No. Import sorting is isort's job, not Black's, and the two disagree often enough that doing it here would surprise you.

**Can it format a file with a syntax error?**

No formatter can - an unparseable file has no structure to rewrite. You get the line, the column and the surrounding code so you can fix it and try again.

**Does it follow PEP 8?**

Black is PEP 8 compatible with one well-known deviation: its default line length is 88 rather than 79. Pick 79 in the line length selector if you need the stricter figure.

**How large a file can I format?**

Up to 400,000 characters per run. Formatting stops after 10 seconds, which only very large or deeply repetitive files reach, and each IP address can run 30 formats every 10 minutes.

**How do I get the same formatting in my editor, pre-commit or CI?**

This page runs Black, and its version is shown in the page header on wide screens. Black reads project settings from pyproject.toml under [tool.black], using the long option names: line-length = 88 for the "Line length" you picked, skip-string-normalization = true if you ticked "Keep my quotes", skip-magic-trailing-comma = true for "Ignore magic trailing comma", and target-version = ["py311"] for a fixed "Target". Editor plugins and the pre-commit hook read the same file. Run black . to format and black --check --diff . in CI: it changes nothing and exits with code 1 when a file would be reformatted. Pin the same Black version as this page, because the first release of each calendar year may change the stable style.

**Is the output the same as Ruff's formatter (ruff format)?**

Almost always, but not byte for byte. Ruff's formatter is designed as a drop-in replacement for Black, and Ruff's documentation reports that on large Black-formatted projects such as Django and Zulip more than 99.9% of lines come out identical. It differs more on code that was never Black-formatted, especially around end-of-line comments, and its documentation lists its intentional deviations. Its default line length is also 88, set with line-length under [tool.ruff]; quote-style = "preserve" under [tool.ruff.format] matches "Keep my quotes" and skip-magic-trailing-comma = true matches "Ignore magic trailing comma". If your CI runs ruff format --check, test the file with Ruff, not with this page.

## More in Developer Tools

- [HTML Viewer](https://discuvr.org/html-viewer/)
- [IP Subnet Calculator](https://discuvr.org/ip-subnet-calculator/)
- [Encode / Decode](https://discuvr.org/encode-decode/)
- [Unix Timestamp Converter](https://discuvr.org/timestamp-converter/)

## Often used with

- [Text Compare](https://discuvr.org/text-diff/)
- [Case Converter & Line Tools](https://discuvr.org/case-converter/)
- [Study Buddy](https://discuvr.org/study-buddy/)

---

Part of Discuvr (https://discuvr.org/): Discuvr is a collection of free, focused tools for day-to-day professional work - office and data utilities, legal and compliance helpers, privacy and security checks, and developer formatters. Every tool has its own page, and most can also be called from ChatGPT or Claude over MCP.
