Python Formatter: Format Python Code in PEP 8 Style
Re-indent, respace, and wrap Python 3.12 code in a PEP 8, Black-inspired style. A full tokenizer checks every result token by token, and the formatter refuses rather than change what your code means. Everything runs in this page; nothing is uploaded.
Ctrl+Enter or Cmd+Enter formats. Tab moves focus out of the editor. Files up to 2 MB are read locally.
Formatted Python
The result appears here after a successful format and safety check.
Problems
No problems found yet.
Rules applied
Format some code to see which rules changed it.
Changes
A unified diff of input versus output appears here. Lines start with + (added) or - (removed); a dotted middle dot marks trailing spaces and an arrow marks a tab.
How it works, and why this is not Black
This is an honest, token-based formatter written in plain JavaScript. It is inspired by Black and PEP 8 but it is not Black or Ruff, and its output will sometimes differ from theirs. There is no parser and no AST: every decision comes from the token stream and bracket structure.
- A tokenizer that follows Python 3.12
tokenizerules reads INDENT and DEDENT, implicit joining inside brackets, backslash continuations, every string prefix (r, b, u, f, rb, br, fr, rf in any case), triple quotes, PEP 701 f-strings with nested quotes, comments, and all number forms including underscores, hex, octal, binary, and imaginary. - It re-indents blocks, normalizes spaces around operators, commas, colons, slices, keyword arguments, and annotated defaults, sets blank lines around definitions, puts two spaces before inline comments, strips trailing whitespace, and ends the file with one newline.
- Long bracketed expressions are split one element per line with a trailing comma; an existing magic trailing comma keeps a bracket exploded.
- Safety check: the output is tokenized again and its significant tokens (names, numbers, operators, string values, NEWLINE, INDENT, DEDENT) must match the input exactly, apart from trailing commas whose removal cannot change meaning. Comments must match in order. If anything differs, the tool refuses and shows your original code.
- Privacy: code stays in this page. No network requests and no browser storage are used.
Known limits, stated plainly:
- It never adds or removes parentheses, so a long line with no brackets stays long, backslash continuations are kept as written, and redundant parentheses are not removed.
- It does not split
a; bor one-line compound statements such asif x: y, does not sort imports, and does not lint. - String contents are opaque: docstrings are not re-indented, f-string internals are untouched, and numeric literal or prefix case is not changed. Quotes change only when the body needs no new escapes; f-strings change only when they contain no double quotes or backslashes.
- Statements with backslash continuations,
# type:comments, or comments that cannot be placed safely keep their original line breaks.# fmt: skipkeeps a statement verbatim;# fmt: offand# fmt: onwork only at the top level. - Width counts code points, and trailing comments do not count toward the split decision. Lines that cannot be split safely are reported as warnings.
- Syntax checking stops at tokenizer errors and indentation structure. Other syntax errors are not detected, but they are carried through unchanged token for token. Python 2 code and Python 3.14 template strings are not supported.