Python Formatter

Paste messy Python and get it back with even indentation, clean line breaks, and consistent spacing. It uses Ruff, which follows the same style as Black. Everything runs in your browser, so your code stays on your device.

Indent
Quotes
Line width
Blank lines

Format uses Ruff, which follows the same style as Black. Options apply to Format.

Result

What the Python formatter does

Python can end up with uneven spacing after edits, merges, or a quick copy and paste. A formatter rewrites the code with even indentation, one statement per line, and consistent spacing, so the structure is clear at a glance. It keeps the behavior of your code and only changes the layout. This tool formats your code with Ruff, which follows the same style as Black.

Why use our Python formatter

  • ·Free. No signup, no paywall.
  • ·Runs in your browser. Your code is never uploaded, so it is safe for private projects.
  • ·Keeps your behavior. Only spacing, quotes, and line breaks change. Your logic stays the same.
  • ·Powered by Ruff. The output matches the Black style that most Python projects already use.
  • ·Syntax highlighting. The editor colours your code so it is easy to scan as you work.

Python formatting options

Clean indentation

Rewrites your code with even indentation and one statement per line, so nested blocks line up and the structure is clear. Choose 2 or 4 spaces per level.

Beforedef f(x):return x+1Afterdef f(x): return x + 1

Quote style

Make string quotes consistent. Prefer double or single quotes across the file, or leave them as written. The formatter may keep the other quote to avoid extra escapes.

Beforename = 'Ada'Aftername = "Ada"

Line width wrapping

Wrap long lines where it can, such as calls, lists, and arguments, so nothing runs off the edge. Set the width to 79, 88, 100, or 120 characters.

Beforeresult = func(aaaa, bbbb, cccc, dddd, eeee, ffff, gggg)Afterresult = func( aaaa, bbbb, cccc, dddd, eeee, ffff, gggg )

PEP 8 style spacing

Fixes spacing around operators, commas, and colons to match the common Python style. Extra blank lines and trailing spaces are cleaned up too.

Beforea=[1 ,2, 3]Aftera = [1, 2, 3]

When to format Python

A formatter helps any time code is hard to read. These are the cases that come up most:

  • ·Cleaning up before review. Format code so a teammate can read it in a pull request.
  • ·Fixing spacing after a merge. Reset uneven indentation and spacing to one consistent style.
  • ·Cleaning up a snippet. Fix the spacing on code you pasted from a chat or a web page.
  • ·Learning Python. See how code breaks into clear lines while you are still learning.

Run Ruff on your own Python project

This tool formats with Ruff, which follows the same style as Black. The result is what most Python projects now expect. To format your own code:

ToUse
Install Ruffpip install ruff
Format every fileruff format .
Check in CI without changing filesruff format --check .
Save your settingsSet line-length under [tool.ruff] and quote-style under [tool.ruff.format] in pyproject.toml

Ruff's default line length is 88 characters, the same as Black. Black and Ruff produce nearly identical output, so a team can switch between them without reformatting everything.

For JavaScript, use theJavaScript formatter and beautifier. For JSON data, use the JSON formatter and validator. For Bash and sh scripts, use theshell script formatter.

Python formatter questions

What is a Python formatter?
A Python formatter takes code with messy spacing and rewrites it with even indentation, consistent spacing, and clean line breaks. It does not change what the code does, only how it looks, so it is easier to read, review, and debug.
How do I format Python code?
Paste your code into the input box, pick your indentation, quote style, and line width, then click Format. The clean version appears below. Click the copy button to copy it, or download it as a .py file.
What does it use to format the code?
It uses Ruff, a fast Python formatter that follows the same style as Black. Ruff is compiled to WebAssembly and runs in your browser, so the output matches what those tools produce, with no setup and no install.
Does formatting change what my code does?
No. The formatter only adjusts spacing, indentation, quotes, and line breaks. It keeps the behavior of your code exactly. It does not run your code, so it works even if the code would fail at runtime, as long as it parses.
Is my code kept private?
Yes. Formatting runs fully in your browser. This tool does not upload or store your code. It downloads its formatter when you first use Format, so the first use needs an internet connection.
Which options can I change?
You can set the indentation to 2 or 4 spaces, choose whether to prefer double or single quotes (or leave them as written), and pick a line width of 79, 88, 100, or 120 characters. The options apply when you click Format.