Quickly turn multiple lines into comments in Python

Trying to comment out a block of code in Python? There are multiple easy methods to do so! You can use the hash character # or turn the lines into a string. The keyboard shortcut for turning lines into code varies depending on the text editor you’re using. This wikiHow shows you how to comment out multiple lines in Python.

Things You Should Know

  • Place a # and a space before each line of code.
  • Alternatively, place """ before and after the block of code.
  • To use a keyboard shortcut, select the block of code, then press the key combination.
Section 2 of 3:

Using a Multiple-Line String

  1. Type """ before and after the lines. This turns the lines into a multiple-line string called a docstring. When a string isn’t assigned to a variable, Python will skip it when running the code.[2]
    • You can also use three single-quotes instead (''').
    • Docstrings are typically used for programming documentation and code summaries. Python’s style guide recommends using the hash character (#) for multi-line block comments.[3]
    • If you’re just getting started with Python, check out how to turn integers into strings.

About This Article

Kyle Smith
Written by:
wikiHow Technology Writer
This article was co-authored by wikiHow staff writer, Kyle Smith. Kyle Smith is a wikiHow Technology Writer, learning and sharing information about the latest technology. He has presented his research at multiple engineering conferences and is the writer and editor of hundreds of online electronics repair guides. Kyle received a BS in Industrial Engineering from Cal Poly, San Luis Obispo. This article has been viewed 2,994 times.
How helpful is this?
Co-authors: 3
Updated: February 28, 2023
Views: 2,994
Categories: Python
Advertisement