Skip to main content
Guides

How to Format Python Code Online (Black, autopep8, YAPF)

By Mithun··6 min read
Quick Answer

Paste your code into an online Python formatter and pick a style — no install needed. The three standard tools differ: Black is opinionated and non-configurable (it even converts single quotes to double), autopep8 only fixes PEP 8 violations and otherwise leaves your style alone (least invasive), and YAPF is configurable to match a house style. I ran the same messy file through all three — the results below show exactly how they differ.

Messy indentation, missing spaces, inconsistent quotes — a formatter fixes all of it in one pass so you can stop arguing about style in code review. The three mainstream Python formatters (Black, autopep8, YAPF) each take a different philosophy, and the quickest way to use any of them is in your browser, with nothing to install.

Here's the same deliberately-messy script formatted by each one (real output, current versions), what sets them apart, and how to pick.

The same messy code, three formatters (tested)

Here's the input — bad spacing, mixed indentation, combined imports, inconsistent quotes:

import os,sys
def add(a,b) :
result=a+b
return result
x = { 'a':1,'b':2 }
class foo:
def bar(self):
return add(1,2)
messy.py

Black (26.5.1) — the opinionated one. Note it rewrote the quotes to double and applied its fixed two-blank-lines-between-defs rule:

import os, sys
def add(a, b):
result = a + b
return result
x = {"a": 1, "b": 2}
class foo:
def bar(self):
return add(1, 2)
black output

autopep8 (2.3.2) — the minimalist. It only fixes PEP 8 violations, so it split `import os,sys` into two lines but kept your single quotes and touched as little as possible:

import os
import sys
def add(a, b):
result = a+b
return result
x = {'a': 1, 'b': 2}
class foo:
def bar(self):
return add(1, 2)
autopep8 output

YAPF (0.43.0) — the configurable one. It kept single quotes but applied its own rules (note the blank line it inserted inside the class):

import os, sys
def add(a, b):
result = a + b
return result
class foo:
def bar(self):
return add(1, 2)
yapf output

What sets them apart

Blackautopep8YAPF
PhilosophyOpinionated, one styleFix PEP 8 onlyConfigurable
QuotesRewrites to doubleLeft as-isLeft as-is
Configurable?Barely (line length)Aggressiveness levelsFully (style presets)
InvasivenessHigh (reflows everything)LowestMedium
Best forTeams wanting zero debateMinimal, safe cleanupMatching a house style

The big practical split: Black reformats your whole file to its style (great for ending style debates, but the diff is large), while autopep8 changes only what actually violates PEP 8 (small, safe diffs on an existing codebase). YAPF sits in between and bends to a config.

Method 1 — format online (no install)

The fastest way, with nothing to set up: paste your code into the free Python Formatter, choose Black, autopep8, or YAPF, and copy the result. It runs in your browser, so your code isn't uploaded — handy for a quick cleanup or when you can't install tools on the machine you're on.

Method 2 — install and run

For your own projects, install the one you like and run it (ideally as a pre-commit hook or CI check so formatting is automatic):

# Black — opinionated, zero config
pip install black && black .
# autopep8 — minimal PEP 8 fixes
pip install autopep8 && autopep8 --in-place --recursive .
# YAPF — configurable
pip install yapf && yapf --in-place --recursive .
pick one

If you're also linting, Ruff now includes a Black-compatible formatter (ruff format) that's dramatically faster and covers both jobs — worth a look if you want one tool. Try it in the online Ruff linter. See also autopep8 vs Black for a deeper head-to-head.

Which should you use?

  • Black — if you want to end style arguments entirely. One style, (almost) no config, huge adoption. The diff on first run is big, then never again.
  • autopep8 — if you have an existing codebase and want the *smallest safe* change: it only fixes real PEP 8 violations and preserves your quotes and layout.
  • YAPF — if your team has a specific house style you want enforced via config rather than accepting Black's opinions.
  • Ruff format — if you want formatting *and* linting from one very fast tool (Black-compatible output).

Format your Python — free

Paste your code, pick Black, autopep8, or YAPF, and copy the clean result. In your browser, nothing uploaded.

Open the Python Formatter

Free tools mentioned here

Related guides

Frequently asked questions

How do I format Python code online?

Paste your code into an online Python formatter, pick a style (Black, autopep8, or YAPF), and copy the cleaned result — no installation needed. pyobfuscate.com's formatter runs in your browser, so your code isn't uploaded. For your own projects, install the formatter (e.g. pip install black) and run it as a pre-commit hook.

What is the difference between Black, autopep8, and YAPF?

Black is opinionated and reformats your whole file to one fixed style (and rewrites single quotes to double). autopep8 only fixes actual PEP 8 violations, making the smallest, least invasive change and preserving your quotes. YAPF is configurable to match a specific house style. Black ends style debates; autopep8 gives safe minimal diffs; YAPF bends to a config.

Does Black change my quotes?

Yes. Black normalizes string quotes to double quotes by default (you can opt out with --skip-string-normalization). autopep8 and YAPF leave your existing single/double quotes as they are, which is one visible difference when you run the same file through each.

Is it safe to format code in an online tool?

It's fine as long as the formatter runs client-side so your code isn't uploaded — pyobfuscate.com's formatter processes everything in your browser. Formatting never changes behavior, only whitespace, line breaks, and (with Black) quote style, so the output runs identically to your input.

Should I use Ruff instead?

Ruff now ships a Black-compatible formatter (ruff format) alongside its very fast linter, so if you want formatting and linting from a single tool it's a strong choice. If you only need formatting and already use Black, there's no urgency to switch — the output is compatible.

Keep reading