Send the result to: Python Formatter Python Validator

About the Python Minifier

What is a Python minifier?

Minifying Python means removing everything the interpreter does not need, which means comments, docstrings, blank lines, and any indentation wider than a single space per level. The result runs identically but takes up less space, which matters when a script is downloaded over the network, packed into an asset, or pushed to a device with little room to spare.

Because Python reads its structure from indentation, a Python minifier cannot simply delete every space the way a JavaScript or CSS minifier can. This one reads your code as a stream of tokens, keeps exactly the indentation the code needs, and shrinks each level to one space. It can combine several simple statements onto one line with semicolons and pull a short block up onto its header line, and it only ever pushes two tokens together when that cannot change their meaning.

There is one deliberate limit. It does not rename your variables. Renaming can save more bytes, but in Python a name can be reached through a global lookup, getattr, or a string passed to eval in ways no minifier can see. This tool keeps code that still works ahead of the last few percent.

How to Use This Tool

  1. Paste your Python. Drop it into the left pane or use Open file to load a .py file. It minifies as you type.
  2. Choose what to strip. Keep Combine statements on to pack simple lines together, and Remove docstrings on to drop them. Turn either off to be more conservative.
  3. Watch the size stats. The counter above the output shows the before and after size and how much you saved.
  4. Ship it. Copy the result or download it as a .min.py file. Switch Mode to Beautify to expand it again.

Common Use Cases

  • Fitting code onto small targets: MicroPython and CircuitPython boards run Python with very little storage, and every comment counts.
  • Shrinking scripts for download: A smaller file is a faster transfer when a script ships as part of a page or a bundle.
  • Stripping comments before sharing: Useful when internal notes and docstrings should not travel with the code.
  • Packing a snippet into one field: Combine mode turns a short module into a compact one-liner-heavy form that pastes anywhere.
  • Measuring real size: See what a script actually costs once comments and formatting are gone.

Need the opposite? The Python Formatter expands minified Python back into readable code. See also the JavaScript Minifier and CSS Minifier, or the rest of our free developer tools.

Frequently Asked Questions

Will minifying break my script?

It should not. Only comments, docstrings, blank lines, and extra whitespace are removed, and every remaining token comes out in the same order. Indentation is preserved as one space per level, so the block structure Python relies on stays exactly as it was.

Does it rename variables to save more space?

No, and that is deliberate. Renaming can save more bytes, but a name in Python can be reached through a global lookup, getattr, or code built at runtime and passed to eval, none of which a minifier can see. Removing comments, docstrings, and whitespace is the part that is always safe.

What do the two options do?

Combine statements joins simple lines together with semicolons and lifts a short block onto its header line, so if x: and its one-line body share a line. Remove docstrings drops the module, class, and function docstrings, replacing one that is a block's only statement with pass so the block stays valid. Turn either off to keep the code closer to its original shape.

How much smaller will my file get?

It depends on how heavily documented the source is. Code with generous docstrings, comments, and four-space indentation often drops by 40 to 60 percent, while code that is already compact may only lose a little. The stats above the output show the exact figure for your file.

Are docstrings and comments really gone for good?

Yes. Once removed they cannot be recovered, so keep the original source. If the file begins with a #! shebang line, that one is kept, since removing it would stop the script running directly.

Is my code uploaded anywhere?

No. The tokenizer and minifier are plain JavaScript running in your browser, so your code never leaves the machine and the tool keeps working offline once the page has loaded.