PDF download Download Article

Quickly turn multiple lines into comments in Python

PDF download Download Article

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 1 of 3:

Using the Hash Character

PDF download Download Article
  1. The hash character is used in Python for single-line and multiple-line comments. Python is a great programming language for beginners and experienced programmers. Comments are an essential part of learning to program in Python . [1]
  2. Advertisement
Section 2 of 3:

Using a Multiple-Line String

PDF download Download Article
  1. 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 .
    EXPERT TIP

    Kevin Burnett

    Software Developer
    Kevin Burnett is a Software Developer with over 20 years of professional experience. He works for SpiffWorkflow, a Python Process Automation tool for organizations. Prior to SpiffWorkflow, he spent the majority of his career at Rosetta Stone, a language-learning software company. He has experience with both front and back-end development and works primarily in Python, Ruby, and JavaScript. He attended the first RailsConf and the most recent PyCon US. He received an MBA and a BA in Computer Science and Spanish from Eastern Mennonite University.
    Kevin Burnett
    Software Developer

    Python commenting scales strategies. For commenting Python, use # symbols and triple quotes. # contains small chunks well, while """ """ wraps multiline snippets cleanly. The former scales better as comments lengthen, improving readability.

Section 3 of 3:

Keyboard Shortcuts

PDF download Download Article
  1. Many text editors include a keyboard shortcut for commenting out multiple lines of code. Select the code and press the shortcut to turn the selected lines into comments. Here are shortcuts for common text editors:
    • Visual Studio: Press Ctrl + K then Ctrl + C
    • Spyder IDE: Ctrl + 1
    • IDLE: Alt + 4
    • Jupyter Notebook: Ctrl + /
    • PyCharm: Ctrl + Shift + /
  2. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How to apply # on all lines of code?
    Bhoimus
    Top Answerer
    First, select the entirety of the code using Ctrl+A. Then, use the keyboard shortcut for commenting code set for your IDE. Default key combinations for popular IDEs are given in the last step above.
Ask a Question
      Advertisement

      Video

      Tips

      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about computer programming, check out our in-depth interview with Kevin Burnett .

      About This Article

      Thanks to all authors for creating a page that has been read 73,215 times.

      Is this article up to date?

      Advertisement