Python Code Indenter & Prettifier
Paste messy Python code to fix indentation, normalize spacing, and prettify with custom indent size
Paste messy Python — mixed tabs and spaces, drifted indentation, code grabbed from a forum — and get it back with uniform spacing in one click. Pick 2 or 4 spaces, hit Format, and the tool re-indents every block based on Python's actual structure: colons, def/class keywords, return statements. And everything runs in your browser. Nothing gets uploaded.
How It Works
The formatter reads each line of your Python code and tracks indentation level based on syntax cues — colons that open blocks, return/break/pass that close them, and def/class keywords that start new scopes. It strips the old whitespace and applies your chosen indent width uniformly. No AST compilation, no server round-trip. Just string-level parsing that handles 95% of real-world formatting issues.
Examples
Fixing Mixed Tabs and Spaces
Copied from a blog post that used tabs — your editor uses spaces.
def greet(name):
if name:
print("Hello, " + name)
else:
print("Hello, stranger")
def greet(name):
if name:
print("Hello, " + name)
else:
print("Hello, stranger")
Reformatting a Class with Nested Methods
When you've been editing a class and the indentation drifted over time. Happens more than anyone admits.
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return "Woof!"
def fetch(self, item):
return f"{self.name} fetched {item}"
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
return "Woof!"
def fetch(self, item):
return f"{self.name} fetched {item}"
Quick Script Cleanup
A short script pasted from a terminal session.
for i in range(10):
if i % 2 == 0:
print(i)
for i in range(10):
if i % 2 == 0:
print(i)
Who Uses This
Cleaning up code before a pull request or after merging conflicting branches.
Fixing indentation errors that cause cryptic "IndentationError" messages in homework assignments.
Reformatting student code samples to a consistent style before grading or presenting in slides.
Tidying Jupyter notebook cells that got messy after dozens of quick edits and experiments.
Common Mistakes
Python 3 throws a TabError if you mix them. This formatter converts everything to spaces, which is what PEP 8 recommends anyway. Paste, format, done.
The formatter won't add a missing colon for you — it only fixes whitespace. If your code has a syntax error like def foo() instead of def foo():, the logic structure won't be detected correctly. Fix syntax first, then format.
This is an indentation fixer, not a linter. It won't rename variables, break long lines, or rearrange imports. For that, you'd want Black or autopep8 installed locally. This tool does one thing well: clean indentation.
Technical Details
- Processing: 100% client-side — nothing leaves your browser
- Browser Support: Chrome, Firefox, Safari, Edge
- Indent Options: 2, 4, or 8 spaces (default is 4)
- Syntax Highlighting: Keyword-aware coloring in the output panel
- Max File Size: Limited only by your browser's memory
Frequently Asked Questions
How do I fix Python indentation errors?
Paste your code here and click Format. The tool strips existing whitespace from each line and re-applies consistent indentation based on Python's block structure. Most IndentationError and TabError issues disappear after one pass.
Does this tool use Black or autopep8?
No. It runs a lightweight JavaScript-based parser directly in your browser. There is no Python runtime involved and no server processing. The trade-off is that it only handles indentation — not line length, import sorting, or other style rules that Black covers.
Is 4 spaces the right indent size for Python?
PEP 8, Python's official style guide, recommends 4 spaces per indentation level. That is the default here. Some teams use 2 spaces for deeply nested code, but 4 is the standard you'll see in almost every open-source Python project.
Can I format Python code on my phone?
Yes. The tool is fully responsive and works on mobile browsers. Paste your code, tap Format, and copy the result. It is handy when you spot a bug on your phone and need to read the code with proper indentation.
Is my code private when I use this?
Completely. The formatting happens in JavaScript inside your browser tab. No code is transmitted to any server. You can disconnect from the internet and the tool still works — that is the simplest proof that nothing is being sent anywhere.