See Ash Code logo See Ash Code

A hash inside a string is not a Python comment

Ashlynn Antrobus
• • 2 min read

A hash character can start a Python comment. It can also be part of a URL, a string, or a docstring.

A hash symbol inside a circle.

Hash icon by Oppous,llc and AntiCompositeNumber, CC BY-SA 4.0; unmodified.

My January comment-removal hook needs to tell those cases apart. Removing everything after every # would damage source code I meant to keep.

For Python, I used the standard library’s tokenizer. It gives the transformation more context than a search for the character alone.

The same character, different tokens

Here is a small teaching input:

#!/usr/bin/env python3
# coding: utf-8
"""Keep # in documentation."""
url = "https://example.com/page#details"
value = 3  # ordinary explanation
# another ordinary comment

The URL’s fragment identifier belongs to the string. The hash inside the triple-quoted text also belongs to a string token. Neither is a COMMENT token.

The tokenizer does identify the last two comments as comments. It also identifies the shebang and coding declaration that way, so my code needs explicit rules to keep those.

For this input, the Python transformation retains:

#!/usr/bin/env python3
# coding: utf-8
"""Keep # in documentation."""
url = "https://example.com/page#details"
value = 3

The removed whole-line comment leaves its line break in the source. The example above omits that trailing empty line so the retained code is easier to see.

Choose tokens, then edit their spans

The implementation encodes the source, calls tokenize.tokenize, and examines the resulting tokens. It collects the positions of comments to remove rather than rebuilding the program from a list of strings it has guessed about.

The exceptions are deliberately narrow: a shebang at the beginning of the first line, and a comment in the first two lines containing coding:.

Python docstrings survive because they are string tokens. The hook isn’t deciding which documentation is useful; it isn’t targeting that token type in the first place.

For each affected line, the code applies deletions from the rightmost position toward the left. Deleting a later span first keeps an earlier span’s recorded offset usable. It then removes trailing whitespace from the changed line while retaining its line ending.

If tokenization raises TokenError, this function returns the original source. It doesn’t fall back to stripping hashes from text it couldn’t tokenize.

The tokenizer doesn’t decide policy

This solves the lexical question of which text Python recognizes as comments. It doesn’t establish that every recognized comment should be deleted.

A comment can contain a tool directive or explain a decision that the code can’t express. I still need to decide which cases this hook should preserve. Those policy decisions deserve examples and tests of their own.

Now I can remove the comment beside a URL without losing the fragment inside the string.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.