Learning how to write comments in Python 3 is one of the most valuable skills you can develop as a programmer — good comments make code readable, maintainable, collaborative, and future-proof. Comments are lines ignored by the Python interpreter, used purely to explain what the code does, why it exists, or how it works.

In this up-to-date 2025–2026 guide, you’ll master how to write comments in Python 3: single-line, multi-line/block, inline, docstrings, commenting out code for debugging, best practices, common mistakes, and when not to comment. All examples are tested on Python 3.10–3.13.

Key Takeaways – How to Write Comments in Python 3

  • Write comments in Python 3 with # for single-line and triple-quotes (“”” or ”’) for multi-line/docstrings.
  • Comments explain why (intent, decisions) more than what (obvious code usually doesn’t need explanation).
  • Use inline comments sparingly — only for tricky lines.
  • Docstrings (“””) are the standard for functions, classes, and modules — tools like Sphinx, pydoc, IDEs, and VS Code use them for auto-documentation.
  • Comment out code during debugging/testing — but remove dead code before production.
  • Follow PEP 8: comments at same indent level as code; 72-char max line length for comments.
  • Avoid over-commenting obvious code — trust other developers to read Python (it’s very readable).

Prerequisites

  • Python 3.8+ installed
  • Text editor (VS Code, PyCharm, nano, Notepad++)
  • Basic Python knowledge (variables, functions, loops)

Comment Syntax in Python 3

How to write comments in Python 3 is simple — use the hash # for single-line comments. Everything after # on that line is ignored by Python.

Basic examples:

				
					# This is a single-line comment
print("Hello, World!")  # Inline comment after code
				
			

Types of Comments & How to Write Them in Python 3

1. Single-Line Comments

Most common way to write comments in Python 3:

				
					# Initialize counter
count = 0

# Loop through numbers 1 to 10
for i in range(1, 11):
    count += i      # Add current number to total
				
			

2. Multi-Line / Block Comments

Python has no official /* */ block comment syntax — use consecutive # lines or triple-quoted strings (not technically comments, but ignored if not assigned).

Preferred way to write block comments in Python 3 (PEP 8 style):

				
					# This function calculates the factorial of a number.
# It uses recursion for simplicity.
# Note: Not efficient for very large numbers due to stack overflow risk.
def factorial(n):
    if n == 0 or n == 1:
        return 1
    return n * factorial(n - 1)
				
			

Alternative (triple quotes – often used for docstrings):

				
					"""
This is a multi-line comment block.
It is ignored by Python if not assigned to a variable.
Useful for long explanations or temporarily disabling code blocks.
"""
				
			

3. Inline Comments

Use sparingly — only when code is non-obvious:

				
					total = price * quantity  # Subtotal before tax
tax = total * 0.15        # 15% sales tax rate
grand_total = total + tax
				
			

4. Docstrings – The Professional Way to Write Comments in Python 3

Docstrings are triple-quoted strings right after a function/class/module definition — they become the official documentation.

How to write docstrings in Python 3 (Google style – most popular in 2025–2026):

				
					def calculate_area(radius):
    """Calculate the area of a circle given its radius.

    Args:
        radius (float): Radius of the circle.

    Returns:
        float: Area of the circle (π * r²).

    Raises:
        ValueError: If radius is negative.
    """
    if radius < 0:
        raise ValueError("Radius cannot be negative")
    return 3.14159 * radius ** 2
				
			

View docstring:

				
					print(calculate_area.__doc__)
# or in interactive console: help(calculate_area)
				
			

Commenting Out Code (Debugging & Testing)

Temporarily disable lines while debugging:

				
					# print("Debug: x =", x)  # Commented out for production
x = x + 1
				
			

Or use multi-line:

				
					"""
print("Old debug code")
print("More debug")
print("Even more")
"""
				
			

Best practice: Remove dead/commented code before committing — git history keeps old versions.

Best Practices – How to Write Comments in Python 3 (2025–2026 Standards)

  • Explain WHY, not WHAT — bad: x += 1 # increment x good: x += 1 # Adjust retry counter after failed API call
  • Keep comments up-to-date — outdated comments are worse than none.
  • Use PEP 8: 72-char max for comments, align with code indent.
  • Use tools: VS Code auto-generates docstrings (Python extension), Ruff/pylint check comment style.
  • Avoid redundancy — don’t comment obvious code.
  • Use English (or team language) consistently.
  • For complex logic: add small comment blocks explaining algorithm steps.

Common Mistakes to Avoid

  • Over-commenting obvious code → clutters readability.
  • Writing comments instead of clear variable/function names.
  • Leaving old/inaccurate comments after refactoring.
  • Using // or /* */ (C-style) → Python uses # only.
  • Not using docstrings for public functions/classes/modules.

How to Write Comments in Python 3 – FAQ (2025–2026)

  1. How do I write comments in Python 3?
    Use # for single-line and “”” or ”’ for multi-line/docstrings — simplest way to write comments in Python 3.
  2. What is the difference between # and “”” for comments in Python 3?
    # = single-line (ignored by interpreter); “”” = multi-line or docstring (can be accessed via __doc__).
  3. How do I write docstrings in Python 3?
    Triple quotes right after function/class definition — standard way to write comments in Python 3 for documentation.
  4. Should I comment every line when writing comments in Python 3?
    No — only explain non-obvious logic. Good names and structure reduce need for comments.
  5. How do I comment out code in Python 3?
    Prefix lines with # or wrap in “”” — quick way to write comments in Python 3 for testing.

Summary

You now know exactly how to write comments in Python 3: single-line #, block comments, inline, docstrings, commenting out code, best practices, and pitfalls to avoid. Good comments turn good code into great, maintainable code — a skill that pays off for your entire career.

Start adding meaningful comments today — your future self (and teammates) will thank you!

Recommended Next Tutorials

  • Python Docstrings & PEP 257
  • Python Type Hints & Annotations
  • Python Best Practices 2025–2026
  • VS Code Python Extension for Auto-Docstrings
  • Clean Code in Python – Comments & Readability