Python Formatter
Beautify and re-indent Python code instantly.
About the Python Formatter
What is a Python formatter?
Python uses indentation to define its structure, so code that has been minified, generated, or pasted with the wrong indent can be genuinely hard to read, and sometimes hard to run at all. A Python formatter rebuilds that code with consistent indentation, one statement per line, and the spacing recommended by PEP 8, so it reads the way well-kept Python should.
This one works on the token stream rather than by matching patterns in the text, which is what keeps it safe. It reads your code the way Python itself does, so strings, f-strings, byte and raw strings, comments, and numbers are treated as single units and never mangled. It normalizes indentation to your chosen width, adds spaces around operators and after commas, tightens up slices and keyword arguments, and puts blank lines around your functions and classes. Every token you put in comes out in the same order, which means formatting can change how your code looks but not what it does.
How to Use This Tool
- Paste your Python. Type or paste it into the left pane, or use Open file to load a
.pyfile. It formats as you type. - Pick your indent. Two spaces, four spaces, or a tab. Four is the PEP 8 standard and the default here.
- Check the badge. A green badge means the code read cleanly. A red one names the problem and the line it is on, such as an unclosed bracket or a bad indent.
- Take the output. Copy it, download it as a
.pyfile, or send it to the Python Minifier with the button underneath.
Common Use Cases
- Fixing broken indentation: Turn code with mixed tabs and spaces, or a wall of two-space blocks, into consistent four-space Python.
- Reading minified or generated Python: Expand tightly packed code back into something you can follow line by line.
- Cleaning up pasted snippets: Code copied out of a chat window, a PDF, or a notebook often loses its spacing on the way.
- Tidying before review: Format a file first and the real changes in a diff stop hiding behind spacing noise.
- Learning the conventions: Seeing your own code laid out to PEP 8 makes the house style obvious.
Need to go the other way? The Python Minifier strips this back down to ship. You may also want the JSON Formatter or the SQL Formatter, or the rest of our free developer tools.
Frequently Asked Questions
Does it follow PEP 8?
It follows the parts of PEP 8 that come from spacing and indentation. It re-indents to four spaces, puts single spaces around operators and after commas, removes spaces inside brackets and around slice colons, and places blank lines around top-level functions and classes. It does not rename anything, reorder imports, or add missing docstrings, since those go beyond formatting.
Will formatting change what my code does?
It should not. The formatter only changes the whitespace between tokens, and every token you put in comes out in the same order. Strings and comments are copied through untouched. As always, keep the original until you have run the formatted version.
Which versions of Python does it support?
It reads modern Python 3, including f-strings, the walrus operator, type hints, decorators, async and await, and match statements. Byte, raw, and triple-quoted strings are all understood as single tokens.
Does it wrap long lines like Black does?
No. It keeps the line breaks you already have, re-indenting the continued lines under their opening bracket, but it will not split a long line for you or join short ones. If you need that, run a full formatter such as Black or autopep8 locally. This tool is built to fix indentation and spacing safely in the browser.
What does the red badge mean?
It means the code could not be read as valid Python, and it names the reason and line, for example an unclosed bracket, an unterminated string, or an inconsistent indent. The output is left empty rather than showing a guess based on code that does not parse.
Is my code uploaded anywhere?
No. The tokenizer and formatter are plain JavaScript running in your browser, so your code never leaves the machine and the tool keeps working offline once the page has loaded.