Python
code commenting
block comment
programming tutorial
code duplication

How to comment out a block of code in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Python has no dedicated block-comment syntax like some other languages, so developers use practical alternatives. The safest option is prefixing lines with #, while triple-quoted strings are sometimes used temporarily during debugging. Choosing the right method improves readability and prevents accidental behavior changes.

Standard Method: Prefix Lines with #

The canonical way to comment multiple lines is adding # to each line.

python
# result = compute_total(items)
# print("Total:", result)
# send_metrics(result)

This is unambiguous and always treated as comment by Python parser.

Editor Shortcuts for Block Commenting

Most editors can add and remove # for selected lines quickly.

Typical shortcuts:

  • VS Code: Ctrl plus slash on Windows and Linux, Cmd plus slash on macOS.
  • PyCharm: same default toggle behavior.

Using editor toggles is faster and cleaner than manual typing.

Triple-Quoted Strings as Temporary Disable

Developers sometimes wrap code with triple quotes.

python
1"""
2result = compute_total(items)
3print(result)
4"""

This creates a string literal, not a true comment. It can still affect memory and linting behavior, so use cautiously.

Why Triple-Quoted Blocks Can Be Risky

Triple-quoted disabled blocks can introduce subtle issues:

  • They are valid runtime expressions.
  • They can interfere with indentation and scope readability.
  • They may be mistaken for docstrings in certain positions.

For long-term code, prefer line comments or remove dead code entirely.

Commenting Inside Functions and Loops

Keep indentation correct when commenting blocks in nested structures.

python
1def process(items):
2    for item in items:
3        # if item.is_invalid():
4        #     continue
5        print(item)

Misaligned comments can hide logical structure and confuse reviews.

Better Alternative for Feature Toggles

If code is temporarily disabled by condition, consider feature flags or explicit guards instead of large commented blocks.

python
1ENABLE_EXPERIMENT = False
2
3if ENABLE_EXPERIMENT:
4    print("Experimental path")

This keeps code executable and testable without manual uncommenting.

Keep Comments Intent-Focused

Comments should explain why something exists, not restate obvious syntax. For disabled code, add short context note if kept temporarily.

python
# Temporary disable due to API rate-limit incident, remove after fix PR-142.
# send_external_event(payload)

Contextual comments reduce confusion for teammates.

Remove Dead Commented Code Before Release

Large commented blocks in production reduce readability and hide real intent. If code is no longer needed, delete it and rely on version control history.

Version control already preserves previous implementation. Keeping old code inline usually adds noise.

Linting and Style Guidelines

Many teams enforce lint rules against excessive commented-out code. Define team guidelines for:

  • When temporary comments are allowed.
  • Maximum lifetime for disabled blocks.
  • Requirement to add context and follow-up ticket.

This keeps repository clean and maintainable.

Notebook and Script Differences

In notebooks, temporary commenting patterns can differ because cells are often rerun interactively. Even there, prefer clear line comments over large disabled blocks so notebook history stays readable and reviewable.

For shared scripts, treat commented-out logic as short-lived and open a cleanup task if it stays longer than one iteration cycle.

Common Pitfalls

  • Treating triple-quoted strings as permanent block comments.
  • Leaving large disabled code blocks after debugging ends.
  • Adding comments without context on why code is disabled.
  • Breaking indentation clarity in nested structures.
  • Forgetting editor comment toggles and editing slowly by hand.

Summary

  • Python block commenting is done with line prefixes using #.
  • Editor comment toggles are fastest and safest for multi-line blocks.
  • Triple-quoted strings are temporary workaround, not ideal long-term comments.
  • Prefer feature flags or deletion over persistent commented-out logic.
  • Keep comments concise, contextual, and maintainable.

Course illustration
Course illustration

All Rights Reserved.