Python Formatter
Normalise Python indentation to a consistent width without changing blocks.
- Runs locally in your browser
- No upload required
Try it
Supported formats
Privacy
Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.
Normalised the indentation of 4 line(s) of Python at 4-space steps.
How it works
- Detected the smallest original indentation step.
- Mapped each line's original indent level to a consistent 4-space width.
- Left the author's block structure untouched so the code cannot be re-associated.
- Copied triple-quoted strings, comments and bracket-continuation alignment verbatim.
- Because Python is indentation-significant, the formatter preserves every block boundary and never re-blocks code.
- Continuation lines inside brackets keep their original alignment so wrapped expressions stay readable.
- Output is 4 line(s); formatting it again changes nothing.
Methodology
- Detect the smallest positive indentation step in the source.
- Map each line's original leading width to a level and re-emit it at level × the chosen indent size.
- Track triple-quoted strings and bracket depth with a string/comment-aware scanner so those regions are copied verbatim.
Usage
- Paste or type a Python file.
- Choose a 2- or 4-space indent.
- Run the formatter and copy the result.
Examples
These are examples, not current values: they use fixed inputs, while the interactive tool computes live results in your browser.
Nested blocks normalised
{
"text": "def f():\n if x:\n return 1\n return 0",
"indentSize": 4
}Triple-quoted string preserved
{
"text": "x = \"\"\"\n keep me\n\"\"\"",
"indentSize": 2
}Tab indentation converted
{
"text": "def f():\n\treturn 1",
"indentSize": 4
}Limitations
Why indentation-only
Python's block structure is encoded in indentation, so a formatter that re-derived blocks from keywords would risk re-associating code. This tool keeps your blocks and normalises their visual width.
The result is consistent indentation that reads the same across editors and diffs.
Frequently asked questions
- Will formatting change what my code does?
- No. It only rewrites leading whitespace to a consistent width while keeping every block boundary exactly where you put it.
- Why doesn't it reformat long lines?
- Re-wrapping code could change behaviour, so the formatter stays conservative and normalises indentation only.
- Is my code uploaded?
- No. All processing happens in your browser and nothing is sent anywhere.
Sources
- PEP 8 — Style Guide for Python Code — Python Software Foundation
- The Python Language Reference — Lexical analysis — Python Software Foundation
Send feedback
Tell us if this tool helped, report a problem, or suggest a tool. Only the message you type is sent — never the input you used in the tool.
Was this tool useful?
Prefer to write to us directly? See the contact page.