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 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

    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
    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.

Community Q&A

Search
Add New Question
  • Question
    How to apply # on all lines of code?
    Bhoimus
    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
200 characters left
Include your email address to get a message when this question is answered.
Submit
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

You Might Also Like

Use Windows Command Prompt to Run a Python FileUse Windows Command Prompt to Run a Python File
Update Pip Upgrade Pip: Quick Guide for Beginners & Experts
Open a Python FileOpen a Python File
Pip Uninstall Package Uninstall a Python Package With Pip: Step-by-Step Guide
Install Pip on Mac3 Easy Ways to Download & Install Pip on Your Mac
Change the Font Size in Python ShellChange the Font Size in Python Shell
Start Programming in PythonStart Programming in Python
Check Python Version on PC or Mac See the Python Version on Mac, Windows, and Linux
Program a Game in Python with PygameProgram a Game in Python with Pygame
Uninstall PythonUninstall Python
Make a Countdown Program in Python Code a Python Countdown Timer: Step-by-Step Guide
Write a Coin Flipping Program on PythonWrite a Coin Flipping Program on Python
Create Loops in Python Create Loops in Python
Write a Python Program to Calculate PiWrite a Python Program to Calculate Pi
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

Kevin Burnett
Co-authored by:
Software Developer
This article was co-authored by Kevin Burnett and by wikiHow staff writer, Kyle Smith. 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. This article has been viewed 73,130 times.
How helpful is this?
Co-authors: 5
Updated: June 24, 2024
Views: 73,130
Categories: Python
Thanks to all authors for creating a page that has been read 73,130 times.

Is this article up to date?

Advertisement