Python formatting, explained

Ruff Formatter

Ruff's formatter rewrites Python layout in a project. This page shows a typical before and after and separates formatting from lint. It does not run Ruff.

Local processing Free to use No install

Before

def total(items,tax):
    amount=0
    for item in items:
        amount+=item
    return amount+tax

Typical result

def total(items, tax):
    amount = 0
    for item in items:
        amount += item
    return amount + tax

Static example. Markdown Viewer does not run Ruff or upload this code.

Format and lint are different commands

ruff format rewrites Python spacing and line layout. ruff check reports lint findings and can apply a different set of fixes. Installing Ruff does not by itself format a file until format is the command you run. The example is a typical Black-compatible layout for a short function, shown here as a static illustration.

Settings live in the project

Line length and a few style options are project settings, often under the Ruff table in pyproject.toml. This page does not read that file, does not install Ruff, and does not choose a line length for you.

This page does not run Ruff

Paste is not available here because Python formatting belongs in the project environment. Prettier explains a different formatter used on JavaScript and Markdown. Neither page uploads source.

Ruff formatter FAQ

Does this page format Python?

No. It explains the Ruff formatter and shows one static example. It does not execute Ruff or accept a Python upload.

Is Ruff format the same thing as Ruff check?

No. Format changes layout. Check reports lint. A project can use one without treating the other as the same command.

Will Ruff format match Black?

Ruff's formatter is intended to be Black-compatible for the Python it supports. Project settings and the Ruff version you install decide the exact result. This page cannot show your project's result.