Why Python scripts end up over-commented
Python encourages comments more than most languages, which is usually a good thing, until a function has three TODOs from three different people and a note explaining a bug that got fixed two commits ago. Stripping comments is a fast way to see the actual logic when you're trying to understand what a script does, not what someone thought it should do.
It's also handy before pasting a function into a forum post or a bug report where your personal notes to yourself aren't useful context for anyone else.
What gets removed, and what doesn't
Python comments start with # and run to the end of the line. There's no block comment, so multi-line notes are usually written as a docstring instead, a string literal wrapped in triple quotes. Since a docstring is a string, not a comment, it's left alone here. Only genuine # comments come out, and a hash inside a string or an f-string stays put.
Frequently asked questions
Does this remove docstrings too?
No. A docstring written with triple quotes is a string, not a comment, so it's left in place. Only lines and trailing text starting with # are removed.
What about a # inside a string, like a hex color or a hashtag?
Those stay. The parser only strips a # when it's actually starting a comment, not when it shows up inside quotes.
Can I run this on a whole module?
Yes, paste as much as you need. It works through the file line by line and removes every real comment it finds, imports and all.