Loading Python Formatter...
Please wait a moment

How to Fix Python Indentation

In Python the indentation is the structure, so code pasted from a chat, a forum post or a notebook with mixed tabs and spaces can stop working. This page puts it back on the 4-space indentation that PEP 8 asks for, while leaving everything inside your lines alone.

Step 1

Paste or upload your Python code

Paste a script, a module or a single function into the left editor, upload a .py file, or press Sample to load a messy example with 2-space, 5-space and tab indentation.

For example, this function uses 2, 8 and 11 spaces of indentation and has four blank lines in a row:

def greet(names):
  for name in names:
        if name:
           print(f"Hello, {name}!")  # say hi




  return len(names)
Step 2

Read the re-indented code

A moment after you stop typing, the right panel shows every block indented with 4 spaces per level, trailing spaces removed and runs of blank lines cut to two. If a bracket, string or indent is broken, you get the line and column instead of output.

The example above comes back like this. The f-string and the comment are untouched:

def greet(names):
    for name in names:
        if name:
            print(f"Hello, {name}!")  # say hi


    return len(names)
Step 3

Minify if you want it small

Minify drops comments and blank lines and uses one space per indentation level. Press Format to switch back.

Step 4

Copy or download

Copy puts the result on your clipboard; Download saves it as formatted.py (or minified.py). If you want Black's full style on top, run Black afterwards; for turning Python data into JSON, try the Python to JSON converter.

Frequently Asked Questions

What exactly does this formatter change?

Only the whitespace around your lines. Each block is re-indented with 4 spaces per level (tabs and odd widths such as 2 or 5 spaces are converted), spaces at the end of lines are removed, more than two blank lines in a row become two, and the file ends with a single newline. Lines that continue inside brackets or after a backslash keep their position relative to the line they belong to. Nothing inside a line is rewritten.

Is it the same as Black or autopep8?

No. Black also rewraps long lines, changes quotes and adds spaces around operators; this page does none of that. It is closer to the indentation fix you would do by hand. If your project uses Black, run Black before you commit; this page is for quickly cleaning up code you copied from a chat, an email or a notebook.

Can formatting change what my program does?

It should not. Strings, triple-quoted strings (including their inner indentation), f-strings with nested quotes, comments, numbers such as 8901240544102066246 and 14.50, and the text "00501" are copied exactly. Indentation keeps the same nesting, so every statement stays in the same block. We test this by comparing Python's own syntax tree (ast.dump) of the input and the output across the standard library.

Why does it say my code cannot be formatted?

Because Python itself would refuse it. The page checks for unclosed brackets and strings, a closing bracket without a partner, an indented line that does not line up with any outer block, a missing indented block after a line ending in a colon, and tabs mixed with spaces in a way Python calls ambiguous. It does not guess where a block ends when all indentation has been lost, since that guess can change what the code does. It is not a full Python parser, so a typo such as a missing comma can still get through and only shows up when you run the code.

What does Minify do?

It removes comments, blank lines and trailing spaces and indents with a single space per level, which Python accepts. The shebang and the coding line at the top are kept. Docstrings stay because they are part of the program. Minified code is fine for pasting somewhere small; keep the formatted version in your repository.

Is my code uploaded anywhere?

No. Formatting runs in your browser, the code is not sent to our server, and nothing is kept: a reload starts with an empty editor. Uploads are limited to 10 MB, and a file of a few thousand lines is formatted in well under a second.