Python Formatter
Rescale your Python code's indentation to a consistent width, live as you type.
def greet(name):
if name:
print("Hello, " + name)
else:
print("Hello!")Honest about what this does: it rescales your existing indentation to a consistent width — it doesn't parse Python, so it can't fix code that isn't already consistently nested (mixed tabs/spaces at the same level, one-line statements, etc. are outside what it can safely correct). For a full PEP 8 formatter, use a tool like Black locally. Runs entirely in your browser.
Need the opposite? PHP Formatter
What is Python Formatter?
Paste Python code with inconsistent indentation (mixed widths, tabs) and this tool re-expresses it at a consistent 2 or 4 spaces, preserving the nesting your code already has. It's honest about its limits: this doesn't parse Python, so it can't fix code that isn't already consistently structured. For full PEP 8 formatting, use a tool like Black locally.
How to Use the Python Formatter
- 1
Paste the Python Code: Enter Python with mixed or inconsistent indentation.
- 2
Choose Indent Width: Pick 2 or 4 spaces.
- 3
Read the Rescaled Result Instantly: The re-indented code appears automatically as you type.
- 4
Copy the Result: Use the copy button to copy the result.
How It Works
Python has no braces to track nesting by, since indentation itself defines scope. That means this tool works differently from the brace-based PHP/JS formatters. It reads each line's existing indentation relative to the lines around it to figure out its nesting level (how many blocks deep it sits), then remaps every level to a consistent width. Mixed 3-space and 5-space steps in the original, for example, both become uniform 4-space steps, without changing which lines belong inside which block.
Example
def greet(name):
if name:
print(f"Hi {name}")
else:
print("Hello")def greet(name):
if name:
print(f"Hi {name}")
else:
print("Hello")The mixed 3- and 7-space indent steps are remapped to a uniform 4 spaces per nesting level, without changing which lines belong inside which block.
Common Use Cases
Fixing up a snippet copy-pasted from a doc, forum post, or PDF where indentation got mangled, before dropping it into an editor with a fixed tab width, a quick pass without needing Black or a local Python environment installed.
Frequently Asked Questions
Common questions about Python Formatter.