Home » Developer Tools » Python Minifier
Paste Python and get a smaller version in seconds, with indentation kept exactly as Python needs it.
Last updated 13 November 2026
Your code never leaves your browser. Everything runs locally on your device.
Python is different from other languages: indentation is part of the syntax, so a careless minifier that joins lines or strips spaces will break your code. The Python Minifier is built around that rule and never touches indentation.
Instead it removes only what Python does not need to run: comments, docstrings, and blank lines, plus trailing whitespace. The saving is modest, but the output is still valid Python you can run. Everything happens in your browser, so your code is never uploaded to a server.
Here is a real file run through this tool. A test file of 157 characters holds a module docstring that spans two lines, a greet function with a one line docstring, and an empty function whose only body is a docstring. With both removal options on, the output is 66 characters, 58 percent smaller. The module docstring is gone, the greet docstring is gone, and the empty function now reads def empty(): followed by a pass line, so the output still runs.
Before the fix, that same file came out broken: the two line docstring stayed in place and the empty function had no body at all, which Python rejects. The minifier now strips multi line docstrings in full and puts a pass in any block left empty, so the smaller result is still valid Python.
The minifier removes comments, docstrings and blank lines, plus trailing whitespace at the end of each line. It never changes indentation and never joins lines, because in Python indentation and line breaks are syntax. A hash sign inside a string is kept, f strings are kept, and a triple quoted string assigned to a variable is kept, because that string is data, not a docstring. A shebang line and an encoding comment on the first two lines are kept too.
The two options work on their own. Turn off Remove docstrings to keep your documentation and only strip comments and blank lines. Turn off Remove comments to keep your notes and only strip docstrings. With both options off, the tool only trims blank lines and trailing whitespace.
This minifier does not rename variables, so the output keeps your names and stays easy to read. That also means the saving is smaller than a JavaScript minifier gives, because JavaScript can join lines and rename identifiers while Python cannot. It does not join lines, it does not compile your code, and it does not run it, so run the result yourself before you ship it.
It also does not obfuscate. Anyone who reads the minified output can still read your logic, because the names and structure are unchanged. If you need to hide how your code works, use a real obfuscator instead of a minifier.
Yes, it is completely free with no sign up.
Yes. The minifier never changes indentation or joins lines, so blocks and nested code keep their meaning. Only comments, docstrings, and blank lines are removed.
Python uses indentation and line breaks as syntax, so lines cannot be joined with semicolons the way JavaScript lines can. The saving comes from comments, docstrings, and blank lines only.
No. Text inside strings is preserved exactly, including a hash sign inside a string, and triple quoted string content is left untouched.
Yes. Turn off the docstring option and only comments and blank lines will be removed.
No. All minifying happens in your browser on your own device, so your code never leaves your computer.
After the docstring is removed the function would be empty, which is invalid Python, so the minifier puts a pass line in the body. The output still runs, and the function still does nothing, just as before.
Yes. A docstring that spans several lines is removed in full, including its closing quotes, when the docstring option is on. Only the first line was removed before, so the rest of the docstring used to stay in the output.
It keeps indentation untouched and puts a pass in any block left empty, so the output stays valid Python. The tool does not run your code, so run the result yourself before you ship it.
Yes. The two options work on their own. Turn off Remove comments and keep Remove docstrings on, and only the docstrings go.
No. Variable and function names are kept as they are, so the output stays easy to read and debug. That is also why the saving is smaller than a JavaScript minifier gives.
If your Python needs tidying up instead of shrinking, try the Python formatter to normalize its indentation and spacing.