DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

Python Triple Quotes Explained: Why They Didn’t Work as Comments

Triple quotes create Python strings, not comments. Learn the placement rule that makes a string a docstring and how to write ordinary comments.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Triple quotes delimit a Python string; they do not create comments. A string becomes a docstring only when it is the first statement in a module, class, function, or method. For ordinary explanatory notes that Python ignores, use #.

Why triple quotes are not comments

Python treats a comment and a string as different parts of the language. The Python Language Reference defines a comment as starting with a # that is not inside a string literal and ending at the physical line’s end. Comments are ignored by Python’s syntax.

As an Amazon Associate I earn from qualifying purchases.

Three matching single quotes or three matching double quotes, by contrast, delimit a string literal. Triple-quoted strings can contain unescaped newlines, and those newlines are part of the string. The quotes change how the string is written; they do not turn it into a comment.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

When a triple-quoted string becomes a docstring

Placement is what makes a string a docstring. PEP 257 defines a docstring as a string literal that is the first statement in a module, function, class, or method definition. Python makes that docstring available through the object’s __doc__ attribute.

# Python ignores this comment as syntax.

def parse_record(text):
    """Parse one record and return its fields."""
    return text.split(",")

print(parse_record.__doc__)

The function’s docstring sits immediately after its def line, before any other statement. The comment beginning with # is separate and is not exposed as the function’s documentation.

What happens when the string is in the wrong place

A triple-quoted string after another statement is still a string literal, but it is not the function’s runtime docstring. PEP 257 says string literals elsewhere are not assigned to __doc__ or recognized as documentation by the bytecode compiler.

def parse_record(text):
    result = text.strip()
    """This does not become parse_record's docstring."""
    return result

Here, the string follows an assignment, so it does not document parse_record. To document the function, move the intended docstring directly below the def line. To leave a note for readers of the code, use a hash comment.

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

Choose the syntax that matches your intent

Construct Purpose Placement and behavior
# explanation Commentary for people reading the code Place it outside a string literal; Python ignores it as syntax and does not attach it to an object’s __doc__.
"""text""" in an arbitrary position A string literal Can span lines, but does not automatically document an object.
A string literal as the first statement in a module, class, function, or method Documentation for that object Python assigns it to the object’s __doc__ attribute.
A string after a simple assignment at module or class level, or in an __init__ method An attribute docstring, in PEP 257 terminology Not assigned to runtime __doc__; some documentation tools may extract it.
A string immediately following another docstring An additional docstring, in PEP 257 terminology Not assigned to runtime __doc__; some documentation tools may extract it.

Attribute and additional docstrings are specialized conventions for certain documentation tools. For documentation that should be available on an object through __doc__, use the first-statement position.

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

Write comments and docstrings clearly

PEP 8 recommends starting each block-comment line with # followed by a space, and keeping comments clear and current. It recommends docstrings for public modules, functions, classes, and methods, with PEP 257 providing further conventions.

PEP 257 recommends triple double quotes for docstrings, including one-line docstrings. A multiline docstring normally begins with a summary line, followed by a blank line and further detail. This is a style convention, not a special meaning assigned to triple quotes by Python’s syntax.

When useful, a function docstring can explain behavior, arguments, return values, side effects, exceptions, or calling restrictions. Avoid adding one that merely repeats what obvious code already says, and keep both comments and documentation accurate as the code changes.

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

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 *

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.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.