Python comment remover

Remove comments from
Python code.

Remove comments from Python code online. Preserves docstrings by default, handles shebangs, and never breaks string literals that contain # characters.

Free, no signup Runs in your browser Instant, no upload Works on .py files

Before and after

Real-world Python code on the left. The same code with every comment removed on the right.

python-input.py
#!/usr/bin/env python3
"""Process customer feedback CSV exports."""

import pandas as pd  # for data frames
from pathlib import Path

DATA_DIR = Path("data")  # where exports land
COLOR_HEADER = "#ff0000"  # not a comment

def load_feedback(path: Path) -> pd.DataFrame:
    """Read feedback into a dataframe."""
    # Skip the header rows
    df = pd.read_csv(path, skiprows=2)
    df = df[df["status"] != "spam"]  # filter out spam
    return df

# Entry point
if __name__ == "__main__":
    load_feedback(DATA_DIR / "feedback.csv")
python-output.pycleaned
#!/usr/bin/env python3
"""Process customer feedback CSV exports."""

import pandas as pd
from pathlib import Path

DATA_DIR = Path("data")
COLOR_HEADER = "#ff0000"

def load_feedback(path: Path) -> pd.DataFrame:
    """Read feedback into a dataframe."""
    df = pd.read_csv(path, skiprows=2)
    df = df[df["status"] != "spam"]
    return df

if __name__ == "__main__":
    load_feedback(DATA_DIR / "feedback.csv")
Why use it

Built for Python specifically.

Python's # comments are everywhere, and many tutorials over-comment for teaching purposes. When you want to compare two implementations, post a clean snippet on Stack Overflow, or ship a leaner script, removing comments is faster than doing it by hand. The tricky bit is that # appears inside strings, regex character classes, and color codes, all of which Uncommenter correctly leaves alone.

  • Removes # line comments without touching strings
  • Preserves docstrings ("""...""" and '''...''') by default, toggle off to remove them
  • Keeps shebang lines (#!/usr/bin/env python3)
  • Handles raw strings (r"..."), f-strings, and bytes literals
  • Auto-detected from .py, .pyw, and .pyi files
How it works

Strip comments in 30 seconds.

  1. 1

    Open the tool

    Head to uncommenter.com/tool. Nothing to install. Nothing to sign up for.

  2. 2

    Paste your Python code

    Drop your .py file in, or paste code into the editor. Auto-detection picks up Python from the extension or file content.

  3. 3

    Click 'Remove Comments'

    The parser walks every character with a real state machine, strings, regex, and other context-sensitive parts are detected and left alone.

  4. 4

    Copy or download

    Grab the cleaned output. Your code never left your browser.

FAQ

Python questions, answered.

Will it remove docstrings?

+

Only if you ask. The default behavior preserves both """triple-double""" and '''triple-single''' docstrings. Toggle the 'preserve docstrings' option off in the tool, or pass `options.preserveDocstrings: false` via the API.

What about strings that contain a # character?

+

They're untouched. The parser tracks string state, so '#ff0000' or 'http://example.com#anchor' is correctly identified as a string, not a comment.

Does it handle f-strings and raw strings?

+

Yes. f-strings (f"hello {name}"), raw strings (r"\d+"), bytes (b"..."), and combinations of those are all parsed as string literals, so comment-like content inside them is preserved.

Other languages

Working in something else?

Plus 35+ more languages supported in the live tool , including HTML, YAML, Dockerfile, Terraform, Solidity, and more.

Try it on your Python code now.

Free forever. No signup. No upload. Runs entirely in your browser.

Open uncommenter