Technical skill practice

Comments in Python: # Lines, Strings and Unfinished Code

Python comments begin with # outside a string and continue to the end of that physical line. They can explain a decision without changing the calculation. A string literal is still a string expression, and a docstring is documentation attached to a module, class or function in its designated position.

Start with a small, explicit task

Annotate a short label calculation, include a literal hash character in the label, and compile an intentionally unfinished expression inside a controlled check. The broken source is a small string fixture, not the script being run, so you can observe SyntaxError without preventing the rest of the example from executing.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_comments.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

8 batch#4

Return a short display label.

unfinished expression rejected

base = 6
# Reserve two extra slots for manual review.
total = base + 2
label = "batch#4"  # The hash inside the string is ordinary text.
assert total == 8 and label == "batch#4"
def describe():
    """Return a short display label."""
    return label
assert describe.__doc__ == "Return a short display label."
try:
    compile("value = (2 +  # unfinished\n", "<example>", "exec")
except SyntaxError:
    result = "unfinished expression rejected"
else:
    raise AssertionError("expected SyntaxError")
print(total, label)
print(describe.__doc__)
print(result)

Explain the result

The full-line comment explains why two slots are added, while the trailing comment explains why the hash in the quoted label is not a comment. Neither changes total. The function’s first string is available through __doc__, showing a documentation role that an ordinary # comment does not provide.

The compiled fixture contains an opening parenthesis and an incomplete addition. The comment hides only the remaining characters on that line; it does not finish the expression or supply its closing syntax. The example checks the exception type rather than a version-specific error-message string.

Avoid these mistakes

Keep comments consistent when code changes. A stale explanation can mislead even when the program still runs. Triple-quoted strings are not a general multiline-comment syntax: they create string literals and can have a docstring role in the appropriate position.

Questions, answered

Can # appear inside a string?

Yes. batch#4 contains a literal hash because it is inside quotes.

Can a comment fix incomplete syntax?

No. The controlled compile check still raises SyntaxError for the unfinished expression.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.