Python Formatter
Format and beautify Python code with Ruff directly in your browser.
Python Formatter example
Generated by Ruff 0.16.2 with the default 88-character line length, double quotes, spaces, and magic trailing commas enabled.
Example input
def greet(name):
if name:
print("Hello,",name)
else:
print("Hello, world!")
items=[1,2,3,4]
for item in items:
print(item)
Expected output
def greet(name):
if name:
print("Hello,", name)
else:
print("Hello, world!")
items = [1, 2, 3, 4]
for item in items:
print(item)
How to Format Python Code
- 1
Paste Python source, load the tested sample, drag in a file, or choose Upload.
- 2
Select a target Python version, preferred line length, quote style, indentation style, and magic-trailing-comma behavior.
- 3
Choose Format Python or press Ctrl/Cmd + Enter.
- 4
Review the read-only output, then copy it or download a formatted .py file.
What this tool does
SiteSCS sends the source to Ruff running as WebAssembly inside the current browser tab. Ruff parses Python syntax, chooses stable line breaks and indentation, normalizes supported quote and trailing-comma behavior, and returns formatted source.
The input is preserved beside the output so you can review changes before copying or downloading. Formatting does not execute the Python program, upload it for processing, or run Ruff's linter.
Formatting Options
- Ruff 0.16.2 formatter compiled to WebAssembly
- Python 3.9 through 3.14 target selection
- Configurable line length from 40 to 200
- Double, single, or preserved quote preference
- Space or tab indentation
- Magic trailing comma control
- .py, .pyw, and .txt upload with a 750 KB limit
- Optional fragment-based share state
Technical information
The Ruff module and its WebAssembly binary are dynamically imported only after the Python Formatter requests them. Initialization is cached for later formatting during the same browser session; unrelated SiteSCS pages do not load the formatter.
Line length is a preferred wrapping width rather than a guarantee that every output line will fit. Target-version affects syntax assumptions. Quote and indentation choices map directly to Ruff formatter configuration, while magic-trailing-comma behavior maps to Ruff's skip-magic-trailing-comma setting.
Common use cases
- Normalizing a Python file before review
- Formatting copied examples and scripts
- Checking how Ruff will format code under a selected target version
- Producing a clean file for a code diff
- Applying a consistent quote and indentation preference
Limitations
- Formatting does not prove that a program is correct, safe, or free of runtime errors.
- This tool does not execute Python, lint every PEP 8 concern, sort imports, or perform static type checking.
- Invalid or unsupported Python syntax can prevent Ruff from producing output.
- Ruff's WebAssembly API is documented as experimental and can change between versions.
- Line length is best-effort; strings, comments, and some expressions can remain longer than the configured width.
Why format Python?
Consistent indentation and line wrapping make reviews faster and reduce style-only changes in pull requests. A deterministic formatter also lets teams spend less time debating layout and more time reviewing behavior.
Python formatting and PEP 8
Ruff’s formatter is designed as a fast, opinionated formatter with Black-compatible output, while PEP 8 remains a broader style guide. Formatting improves layout, but it does not replace linting, type checking, tests, or human review.
Privacy
Formatting, file reading, copying, and downloading happen in this browser tab. SiteSCS does not upload or store your source. Sharing includes code only when you opt in, and then places a bounded copy in the URL fragment.
Related tools
References & Sources
Documents the formatting engine, supported formatter behavior, configuration surface, and known compatibility notes used by this tool.
Verified 2026-08-13 ↗Astral · Official DocumentationRuff SettingsDefines target-version, line-length, quote-style, indent-style, and magic-trailing-comma settings exposed in the workspace.
Verified 2026-08-13 ↗Python Software Foundation · Official DocumentationPython Language ReferenceThe authoritative reference for Python syntax and language behavior parsed by the formatter.
Verified 2026-08-13 ↗Python Enhancement Proposals · Official DocumentationPEP 8 — Style Guide for Python CodeBackground guidance for Python style conventions; formatting alone is not complete PEP 8 or lint compliance.
Verified 2026-08-13 ↗Python Formatter FAQ
Which formatter does SiteSCS use?
Ruff 0.16.2 compiled to WebAssembly. The formatter module is loaded only when this tool first needs it.
Is formatting the same as PEP 8 compliance?
No. Ruff formatting produces consistent layout, but it does not report every style, correctness, import, or type issue.
Is my Python code uploaded?
Not for formatting. Ruff runs inside the browser. Optional share-state inclusion is off by default and encodes small source directly in the URL fragment rather than storing it.
Why can an output line exceed the selected length?
Ruff treats line length as a preferred wrapping width. Some strings, comments, and expressions are intentionally not split.