Python Formatter

Normalise Python indentation to a consistent width without changing blocks.

  • Runs locally in your browser
  • No upload required

Try it

Paste Python. Indentation is normalised; block structure is preserved.

Formatted Python
def f(): if x: return 1 return 0
Lines
4
def f():
  if x:
      return 1
  return 0

Supported formats

Input
text, number
Output
text

Privacy

Runs entirely in your browser. Your input is never uploaded, stored, or sent to a third party.

Processing
In your browser
Privacy level
Browser only

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

  1. Detect the smallest positive indentation step in the source.
  2. Map each line's original leading width to a level and re-emit it at level × the chosen indent size.
  3. Track triple-quoted strings and bracket depth with a string/comment-aware scanner so those regions are copied verbatim.

Usage

  1. Paste or type a Python file.
  2. Choose a 2- or 4-space indent.
  3. 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

jsonSelect to copy
{
  "text": "def f():\n  if x:\n      return 1\n  return 0",
  "indentSize": 4
}

Triple-quoted string preserved

jsonSelect to copy
{
  "text": "x = \"\"\"\n  keep   me\n\"\"\"",
  "indentSize": 2
}

Tab indentation converted

jsonSelect to copy
{
  "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

  1. PEP 8 — Style Guide for Python Code — Python Software Foundation
  2. The Python Language Reference — Lexical analysis — Python Software Foundation