Format Code Now
/
Web & markupPython Formatter Next: Ruby Formatter ›

Python Formatter and Beautifier

WM-97

Paste Python and change its indentation to the width you choose without changing what the blocks mean. Only the leading whitespace of lines is rewritten.

Runs in your browser. Nothing is uploaded.

Python code

Re-indented Python

How to use it

  1. Paste your Python, or open a .py file or drop it on the input box.
  2. Pick 2, 4 or 8 spaces, or a tab.
  3. Press Re-indent.
  4. Copy the result or download it as a .py file.

What it rewrites

Indentation is meaningful in Python, so the tool works out the block depth of every line and rewrites the leading whitespace to your width. Blank lines are limited to two in a row and trailing spaces are removed. Lines inside triple-quoted strings are left exactly as written, because their spaces belong to the string. Continuation lines inside brackets or after a backslash move by the same amount as the line they continue, so their alignment stays.

A safety check

After re-indenting, the tool compares the new text with the old one and checks that every line has the same depth as before. If that check fails, you get an error and no changed code. The reader follows Python's own rules for mixing tabs and spaces, so files that Python itself would reject are reported rather than guessed at.

What it is not

It is not Black or autopep8. It does not wrap lines, change quotes, sort imports or fix spacing around operators. The old site option for a Python version is not needed here, since the code is not parsed into a syntax tree. Use a full formatter if you want those changes.

Error messages and what to do

MessageWhat it meansFix
This line is dedented to a column that matches no outer block (line 3).A line returns to an indent that no earlier block used.Align the line with an outer block, as Python itself would require.
Unexpected indent at the start of the code; remove the leading spaces (line 1).The first line of code is indented.Remove the leading spaces from the first line.
( is never closed (line 1).A bracket opened and the code ended before it was closed.Add the missing closing bracket.
Tabs and spaces are mixed in the indentation in a way Python rejects (line 3).A block mixes tabs and spaces so that its depth is ambiguous.Use only tabs or only spaces within a block.

Questions people ask

How do I change Python indentation from 2 to 4 spaces?

Paste the code, choose 4 spaces and press Re-indent. Every block is moved to the new width and the structure stays the same. Paste the code, choose 4 spaces and press Re-indent. Every block moves to the new width and the structure stays the same, and the result is checked.

Will it break my code?

The output is checked so every line keeps its block depth. If the check cannot be satisfied, you get an error instead of changed code.

What happens to multi-line strings?

Lines inside triple-quoted strings are not changed, since their spaces are part of the string. Continuation lines inside brackets or after a backslash shift together with the line they continue.

Does it convert tabs to spaces?

Yes, if you choose a space width. Choose Tab to convert spaces to tabs. Mixed indentation that Python would reject is reported. Yes, when you choose a space width, tabs in the indent become spaces. Choose Tab to go the other way. Mixed indentation that Python would reject is reported.

Is this the same as Black?

No. Black rewrites many things, such as quotes and line breaks. This tool only changes indentation depth. No. Black rewrites many things, such as quotes, spacing and line breaks. This tool only changes indentation depth and trailing spaces, and leaves the rest alone.

About this tool

Changes the indentation of Python code to the width you choose and nothing else. It is not Black or autopep8: it does not wrap lines, change quotes, sort imports or add spaces around operators. It reads strings, brackets, backslash continuations and comments so that lines inside them are left alone. Each block keeps its depth, and the result is checked against your input so that the block structure is the same; if that check fails you get an error instead of changed code. Inconsistent tabs and spaces, an unexpected indent and a dedent that matches no outer block are reported with their line.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.