⚡
usefultools.dev
Py

Python Formatter – Fix Indentation & Whitespace

NOUVEAU

Python formatter for safe whitespace cleanup: tabs to spaces, consistent indentation, PEP 8 blank lines and operator spacing, with a diff. Free, runs in your browser.

Fonctionne à 100 % dans votre navigateur — vos données ne sont jamais envoyées ni stockées sur nos serveurs.

Cet outil n'est pas encore entièrement traduit : une partie du texte est encore en anglais. L'outil lui-même fonctionne de la même façon dans toutes les langues.

Clean-up rules

Formatted code

import os


def total(items, tax=0.2, *args, **kw):
    if tax == 0:
        return sum(items)
    result = sum(i * (1 + tax) for i in items)
    return round(result, 2)  # two decimals


class Cart:
    def __init__(self, items: list = None):
        self.items = items or []

    def add(self, item):
        self.items.append(item)


print(total([10, 20]), "a=b, c")

15 lines changed

8 lines re-indented · 4 with tabs · 22 spacing fixes · 1 trailing whitespace removed · 7 blank lines added · 3 blank lines removed

Whitespace-only clean-up. It does not wrap long lines, change quotes or reorder imports like black or ruff format would.

⚡

Exemples rapides

Des entrées typiques et les résultats produits par cet outil.

Operators
if a==b :
if a == b:
Keyword arg
f(key = 2)
f(key=2)
Annotated
def f(x: int=0):
def f(x: int = 0):
Comment
return x # note
return x # note
Comma
add(1,2)
add(1, 2)
📖

Présentation

This Python formatter cleans up whitespace without touching anything else. It converts tab indentation to spaces and rebuilds every indentation level from the block structure, removes trailing whitespace, puts two blank lines around top-level functions and classes and one between methods, and adds the spaces PEP 8 expects around operators, after commas and before inline comments — while keeping keyword arguments like f(key=2) tight.

It is deliberately not black or ruff format. It never re-wraps long lines, never changes quotes, never reorders imports and never changes anything inside a string or comment; even trailing spaces inside a multi-line string are kept. Continuation lines inside brackets move with their statement, so aligned arguments stay aligned.

Because it uses a real Python tokenizer, unterminated strings, unbalanced brackets and inconsistent dedents are reported with a line number instead of being "fixed" into different code. A line-by-line diff shows exactly what changed. Your code never leaves the browser.

Pourquoi utiliser cet outil ?

  • Fix mixed tabs and spaces in seconds
  • Never changes strings, quotes or line breaks
  • PEP 8 blank lines and operator spacing
  • See every change in a line diff
  • Works without installing anything
⭐

Fonctionnalités

↹

Re-indentation

Indentation is rebuilt from the block structure with 2, 4 or 8 spaces, converting tabs.

➕

Operator spacing

Spaces around =, ==, +=, ->, := and binary operators, but not around keyword arguments or unary minus.

📏

PEP 8 blank lines

Two blank lines around top-level def/class, one between methods, none right after a block header.

🧹

Trailing whitespace

Removed from code lines, never from inside multi-line strings.

🔍

Diff summary

Counts of re-indented lines, spacing fixes and blank lines, plus a full line diff.

🛑

Safe on errors

Unterminated strings or bad dedents are reported with a line number instead of guessed.

🛠️

Mode d'emploi

1

Paste your code

Paste a script or a snippet into the Python code box.

2

Choose the rules

Pick the indent size and turn individual clean-up rules on or off.

3

Review the changes

Read the summary and open Show line diff.

4

Copy or download

Press Copy or Download .py.

🤔

Questions fréquentes