Skip to content

Black: Python Code Formatter

Black is an opinionated Python code formatter that rewrites source files to conform to a strict subset of PEP 8. It exposes few configuration options by design, so teams converge on one canonical style instead of debating formatting choices.

When to use Black

Black fits codebases that already run it in CI or pre-commit hooks and have built workflows around its stable output. Its formatting has been the de facto Python standard since 2018, and many editor plugins, linter configs, and CI pipelines assume Black-compatible style.

For new projects, the Ruff formatter produces nearly identical output, runs 10-100x faster, and bundles linting and import sorting in the same binary. To switch an existing project, see How to migrate from Black to the Ruff formatter. To consolidate the wider lint stack at the same time, see How to replace Black, isort, flake8, and pyupgrade with Ruff.

Key Features

Formatting style

Black defaults to 88-character lines, four-space indentation, and double quotes. It adds trailing commas in multi-line constructs, normalizes string prefixes, and collapses or expands expressions to fit the line-length target. The output is deterministic: the same input always produces the same output regardless of the original formatting.

Configuration

Black reads the [tool.black] table in pyproject.toml. The available options are deliberately limited: line-length, target-version, skip-string-normalization, skip-magic-trailing-comma, file/directory exclusion patterns, and a preview flag for upcoming style changes. This scarcity of knobs is the tool’s central design choice.

Integration

Black ships pre-commit hooks, editor plugins (VS Code, PyCharm, Vim/Neovim), and a --check mode for CI that exits non-zero when files need reformatting. It can also format Jupyter notebooks and exposes a Python API for programmatic use.

Pros

  • Deterministic output eliminates formatting debates across teams
  • Near-zero configuration required for most projects
  • Stable style that has changed only once (the 2022 stable release) since the tool’s adoption
  • Broad ecosystem support: most Python linters and editors understand Black-compatible formatting

Cons

  • Does not lint, sort imports, or fix code issues (requires pairing with flake8 or isort for a complete code-quality pipeline)
  • 10-100x slower than Ruff’s formatter on large codebases
  • The few style choices it makes (double quotes, 88-char lines) cannot be overridden without skip-string-normalization, and some choices have no override at all

Learn More

Last updated on