# Python Formatter

> Reformat Python code with the ruff formatter in your browser: Black-compatible output, an 88-character default line length.

Use it online (free, runs in your browser): https://directory.milunir.com/developer-tools/code-formatters/python-formatter/

Paste Python and it is reprinted in the Black code style by ruff's formatter, running in your browser. The dict x = {  'a':37,'b':42, 'c':927} split over two lines becomes x = {"a": 37, "b": 42, "c": 927} on one 32-character line. Line length, quote style and indent width are adjustable; invalid syntax is reported with its line and column.

Python developers use a formatter to end review arguments about spacing, to tidy a snippet pasted from a chat or notebook, and to see what Black or ruff will do to a file before adding them to a project. This page loads ruff 0.17.6 as WebAssembly and runs its formatter on your device, so the code never leaves the browser. Ruff's formatter is designed as a drop-in for Black: on code Black already formatted it changes almost nothing, and its documented defaults match Black's, with double quotes, 4-space indents and an 88-character line. You can set the limit from 40 to 200, prefer single quotes or keep the quotes as written, and switch to 2-space indents. The result shows how many lines changed and the longest line left. It only reformats layout: it does not sort imports, fix lint errors, rename anything or minify code, and a file that does not parse is refused rather than guessed at.

## How to use

1. **Paste or open the code**: Paste Python into the box or open a .py or .txt file; it is read locally.
2. **Set the line length**: Drag the slider or type a value: 88 matches Black and ruff, 79 matches PEP 8, 120 suits wide editors.
3. **Choose quotes and indent**: Double, single or keep the quotes as written; 4-space or 2-space indentation.
4. **Copy the formatted code**: The output updates as you type, with counts of lines in, lines out and lines changed. A syntax error names the line and column to fix.

## Examples

### A messy dict literal

- Python code: x = { 'a':37,'b':42, 'c':927}
- Line length: 88
- String quotes: Double "…"
- Indent: 4 spaces

Result: Formatted code: x = {"a": 37, "b": 42, "c": 927} Lines out: 1 Lines changed: 1

Two ragged lines with single quotes and no spaces after colons collapse into one 32-character line with double quotes, exactly as the Black docs show.

### A 130-character def line

- Python code: def very_important_function(template: str, *variables, file: os.PathLike, engine: str, header: bool = True, debug: bool = False): pass
- Line length: 88
- String quotes: Double "…"
- Indent: 4 spaces

Result: Formatted code: def very_important_function( template: str, *variables, file: os.PathLike, engine: str, header: bool = True, debug: bool = False, ): pass Lines out: 9 Lines changed: 8

The signature is too long even with all parameters on one indented line, so each parameter gets its own line and a trailing comma is added after the last one.

### Re-indent to 2 spaces

- Python code: if x: y = 1
- Line length: 88
- String quotes: Double "…"
- Indent: 2 spaces

Result: Formatted code: if x: y = 1 Lines out: 2 Lines changed: 1

A 4-space block is re-indented with 2 spaces, the style some teams use for notebooks; only the indented line changes.

## How it works

`Ruff formatter, Black code style: parse source into a syntax tree, then reprint it so each logical line fits the line-length limit, splitting at brackets and adding magic trailing commas`

The code is parsed by ruff's Python parser; if parsing fails, nothing is printed and the error position is converted from a byte offset into a line and column. A valid tree is printed back from scratch, so the original spacing has no effect. A statement that fits within the limit goes on one line. If it does not fit, the formatter first tries to move the bracket contents onto one indented line, and if that is still too long it puts each element on its own line with a trailing comma. An existing trailing comma in a collection keeps it exploded, the magic trailing comma rule from Black. Strings are normalised to the chosen quote unless that would add escapes. Long strings and comments are never split, so a line can still exceed the limit; the tool warns when that happens.

Sources:

- [PEP 8 – Style Guide for Python Code](https://peps.python.org/pep-0008/), Python Software Foundation
- [The Black code style](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html), Black (Python Software Foundation)
- [Ruff formatter](https://docs.astral.sh/ruff/formatter/), Astral

## When not to use it

- Do not use it to enforce import order or lint rules; ruff check and isort-style sorting are separate steps.
- Do not paste Python 2 code with print statements; it fails to parse.
- Do not expect minified output; this only produces readable layout.

## Common mistakes

- Setting the limit to 79 here and leaving 88 in pyproject.toml, so the project formatter undoes the change.
- Pasting a snippet with mixed tabs and broken indentation, which is a syntax error rather than a style issue.
- Assuming a long line is a bug: the formatter never splits a long string literal or comment.

## Questions

### Is there a Python beautifier for messy code?

Paste it into the box and the formatter reprints it in the Black style: one space around operators and after commas, double quotes, 4-space indents and lines wrapped at 88 characters. Only layout changes, so the program behaves the same. If the code does not parse, the error message gives the line and column, which usually points at an unclosed bracket or a stray colon.

### Is the ruff formatter the same as Black?

Ruff's formatter is built as a drop-in replacement for Black and produces nearly identical output on code Black has already formatted; Astral reports over 99.9% of lines unchanged on large Black-formatted projects. The defaults match: 88 characters, double quotes, spaces for indentation and magic trailing commas. Small known differences exist in edge cases such as some comment placements.

### What line length does the Python formatter Black use?

Black defaults to 88 characters, about 10% more than the 79 that PEP 8 recommends, because its authors found it gives noticeably shorter files. Ruff's formatter uses the same 88 default. Set the slider to 79 for strict PEP 8 or 100 to 120 if your team uses wide screens, and put the same value in pyproject.toml so editors agree.

### How do I set ruff as the Python formatter in VS Code?

Install the Ruff extension published by Astral, then in settings.json set "editor.defaultFormatter": "charliermarsh.ruff" inside a "[python]" block and turn on "editor.formatOnSave". The extension reads line-length and quote-style from pyproject.toml or ruff.toml, so the output matches what this page produces with the same options.

### How does a Python dict beautifier lay out a long dictionary?

Paste the literal and the formatter keeps it on one line if it fits within the line length; otherwise it puts each item on its own line with a trailing comma. To force the one-item-per-line layout for a short dict, add a comma after the last item before formatting: that magic trailing comma keeps it exploded.

### Can I use the ruff formatter in Neovim?

Yes. Ruff ships a built-in language server, started with ruff server, which Neovim's LSP client can attach to for formatting on save; plugins such as conform.nvim can also call ruff format directly. Either way the settings come from pyproject.toml, so set line-length and quote-style there to match the options chosen on this page.
