How to use the Python code cleaner
- Paste your code into the Python box. The sample mixes tab and 2-space indentation on purpose.
- Choose the Indent: 4 spaces (PEP 8) or 2 spaces.
- Set Max blank lines in a row (0–5, default 2).
- Read the Cleaned Python and the Lines re-indented count, then copy it or download
cleaned.py.
What it does and when to use it
Python treats indentation as syntax, so a file that mixes tabs and spaces, or 2- and 4-space blocks, can fail with TabError or behave in ways that are hard to see. Code pasted from forums and old scripts often has this problem. The cleaner turns all of it into one clean step size without changing a single statement.
To see exactly what moved, paste the before and after into the code diff viewer. For tab-to-space jobs in other languages, the indentation converter works line by line, and the code beautifier re-indents Python alongside many other languages.
How it works
The cleaner reads your code line by line and keeps a stack of the indent widths it has seen:
- Each line’s leading whitespace is measured, with a tab moving to the next multiple of 8.
- Deeper than the top of the stack: a new block starts. Shallower: levels are closed until one matches exactly, or the IndentationError message appears.
- The new indent is the block depth times your chosen size.
- A small scanner follows strings, comments and brackets, so triple-quoted text is copied unchanged and continuation lines keep their offset.
Trailing spaces are trimmed, blank lines over the limit are dropped and the file ends with one newline. Everything runs in your browser.
Worked examples
These are the tool’s own outputs with the default settings.
A 2-space function.
def area(w, h):
if w <= 0:
return 0
return w * h
becomes
def area(w, h):
if w <= 0:
return 0
return w * h
with Lines re-indented: 3, and the spaces after return 0 removed.
A bad dedent. In if a: / x = 1 / y = 2, the last line sits at column 2, which matches no block. The result is “Line 3: this line’s indentation does not match any outer block (Python would raise IndentationError).”
A dictionary over several lines. data = { followed by items indented 8 spaces, then } and x=1, comes back unchanged: bracket contents keep their layout, and x=1 is not re-spaced.
Limits and tips
- Only whitespace changes.
x=1;y = [1,2]stays exactly as written. - Tabs mean 8 columns. In a file that mixes tabs and spaces, a tab line and a 4-space line are different levels, just as Python sees them. If you get an error you did not expect, look for a mixed line there.
- Long lines are not wrapped. Use Black, Ruff or autopep8 for full PEP 8 formatting.
Frequently asked questions
Is this the same as Black or autopep8?
How does it decide the indent level of each line?
Why do I get an IndentationError message?
Are strings and comments safe?
How do we know nothing is uploaded? Test it yourself on the privacy proof page.