Python has four formatters you will see in real projects. They differ less in what the code looks like than in how much they change and how much they let you configure. Here is how to choose.
| Black | Ruff formatter | autopep8 | YAPF | |
|---|---|---|---|---|
| Approach | Reformats everything to one style | Black-compatible style | Fixes PEP 8 violations only | Reformats to a configurable style |
| Configuration | Very little (line length, quotes) | Very little, like Black | Which fixes to apply | Many style knobs, plus pep8 / google presets |
| Diff on first run | Large | Large | Small | Large |
| Speed | Fast | Very fast (written in Rust) | Fast | Slower on big files |
| Install | pip install black | pip install ruff | pip install autopep8 | pip install yapf |
| Run | black . | ruff format . | autopep8 -i -r . | yapf -i -r . |
Black takes your code, parses it, and prints it back in its own style, ignoring how you laid it out (except for the magic trailing comma). That's the point: two developers who format the same code always get the same result, so layout never comes up in code review.
The trade-off is control. You can set the line length and turn off quote normalization, and that's about it. If you don't like a choice Black makes, you live with it. Most teams find that a good deal.
Ruff started as a very fast linter and now includes a formatter designed as a drop-in replacement for Black. Output is almost identical to Black's, and it is much faster on large repositories. If you already run Ruff for linting, using its formatter means one tool and one config file instead of two.
autopep8 doesn't impose a style. It looks for PEP 8 violations — wrong indentation, missing whitespace, overly long lines — and fixes just those. Code that's already reasonable is left as it is.
autopep8 --in-place app.py # safe whitespace fixes
autopep8 --in-place --aggressive app.py # also makes non-whitespace changes
This is useful on a large, old codebase where reformatting every file would bury real changes in the git history. The downside is that two developers can still format the same code differently.
YAPF, from Google, also reformats everything, but you choose the style. Start from a preset and change individual settings in .style.yapf or pyproject.toml:
[tool.yapf]
based_on_style = "pep8"
column_limit = 100
If your organisation has a detailed existing style guide that Black doesn't match, YAPF can usually express it. The cost is that every option is a decision your team has to make and maintain.
Not on the same files — two formatters with different rules will keep undoing each other's changes. Pick one formatter, and pair it with a linter (Ruff, Flake8 or Pylint) to catch problems formatting can't fix, like unused imports or undefined names.
Paste Python into the free formatter and see exactly what Black would change.
Open the Python FormatterNew to formatters? Start with How to Format Python Code.