Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Use Comments in Python

Use # outside a string literal to add a Python comment. See full-line and inline examples, comment style guidance, and how comments differ from docstrings.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start a Python comment with # outside a string literal. The comment runs to the end of that physical line and is ignored by Python, so use it to explain code to people—not to change what the program does.

Write a comment with #

Place a hash character before the text you want to mark as a comment. You can put a comment on a line by itself or after code on the same line:

# Explain the purpose of the next operation.
total = price * quantity

subtotal = price * quantity  # Inline comment after whitespace

A comment ends at the end of its physical line. Python does not treat the comment text as executable syntax. The rule is described in the Python 3.14.8 lexical reference.

Know when # is part of a string

A hash character inside a quoted string is ordinary text, not the start of a comment:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
label = "# is part of this string, not a comment"

Only a # outside a string literal begins a comment. This distinction matters when strings contain values such as labels or text that visibly includes a hash.

Write comments that add useful context

Comments are for readers. The Python tutorial recommends putting them on a line of their own when possible. In practice, use a comment to explain intent or context that is not apparent from the code, rather than restating an obvious operation.

# Explain why this calculation is needed.
total = price * quantity

The comment does not affect the value of total; it only helps someone understand the code. The style recommendation appears in the Python tutorial’s coding-style summary.

Comments and docstrings serve different roles

A docstring is a string literal placed as the first statement in a module, class, or function. It documents that object and can be used by documentation tools; it is not a # comment.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
def greet(name):
    """Return a greeting for one person."""
    return f"Hello, {name}!"

Here, the string is a docstring because it is the function’s first statement. A comment inside the function has a different role: it is ignored as syntax and can clarify the source for readers. Python’s documentation-strings tutorial section explains this distinction.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A special case: source-file encoding comments

There is a narrow exception to the idea that comments are only for human readers: a specially formatted comment on the first or second line of a source file can declare its encoding. Python uses UTF-8 by default, so most code does not need an encoding declaration. See the lexical reference on encoding declarations for the exact format and placement rules.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.