Python Spacing Formatter

Paste a Python snippet to normalize its leading indentation and some common spaces around operators and commas. This is a lightweight text cleanup: it does not parse Python, validate syntax, wrap long lines, sort imports or reproduce Black or Ruff. Always review the result before replacing working source code.

How to use the spacing formatter

  1. 1

    Paste a copy

    Use a copy of the snippet, especially when it contains strings, comments or unusual spacing.

  2. 2

    Choose the output indent

    Select 2, 4 or 8 spaces for each indentation level inferred from the input.

  3. 3

    Format and compare

    The tool adjusts whitespace line by line. Compare the output with the original before copying it.

What this tool actually changes

The formatter works on text one line at a time. It removes existing leading whitespace, estimates an indentation level from groups of roughly four input spaces, and rebuilds that indent with the selected width. It also standardizes spaces around common operator characters and after commas, and collapses repeated spaces in the remaining line.

Input pattern Typical output
x=1+2 x = 1 + 2
items=[1,2,3] items = [1, 2, 3]
an indented line same inferred level with 2, 4 or 8 spaces

Important limitations

This is not a Python parser. It cannot tell code apart from text inside a string or comment, and it does not understand unary operators, slices, type annotations, continuation lines or the special spacing rules used by full formatters. A spacing change can therefore alter a string literal or produce invalid code. The tool also does not check that the input was valid before formatting.

For production projects, run Black or ruff format locally, where the formatter can parse the whole file and your tests can verify the result. This page is best for a quick visual cleanup of a small, simple example.

Privacy and handling

Formatting is performed by the site when you submit the snippet. Do not paste passwords, private keys, access tokens or confidential source code. Use a local formatter for sensitive material.

Frequently Asked Questions

No. It is a small whitespace normalizer and does not parse an abstract syntax tree or implement those formatters’ rules.

No. The result may still contain errors, so run Python, a linter and your tests after making changes.

Yes. Because it works on plain text, its spacing rules may also affect strings and comments. Review the diff carefully.

Related Tools

Tool available in other languages