top of page

How to do comment and to comment out multiple lines in python?

Updated: May 17, 2022


comment out multiple lines in python

Commenting your code or thoughts is necessary for all coding languages. Sometime we are quite lazy with comments, but then we end up regretting it a few weeks, months, or years after as it then is harder than it should be to re-read your code and to understand it as quickly as possible. Adding comments during or on completion of your coding project can really minimize the time it takes for us to understand our own or someone else's code.


Single line comment

Commenting in python can be done with a simple hashtag in front of your code line, #.


For example:

# This is a comment in python.
This is not a comment in python.

Multiline comment

In contrast to many other languages, python does not support multiline commenting, which is very useful for working with blocks of comments where you as a code owner want to write a bit more comments. Fortunately many IDEs (Integrated development environment) such as Spyder, Jupyter notebooks, PyCharm have created quick commands for adding Hashtags so that you don't have to manually add them.


A more creative way to do multiline commenting is by using triple quotation marks and not assigning the text to a variable. That will technically also serve as a multiline comment.


# ====================================================
# My Multiline comment
"""
note that multiline text can be done with triple quotation marks. 
and yes this is an example. 
"""
# ====================================================

Spyder

A single comment in Spyder can be done with ⌘ + 1 (Ctrl + 1 = Windows user).


Multiline comments can be done by selecting several rows and pressing: ⌘ + 4

(Ctrl + 4 = Windows user).


Reversing a multiline comment can be done with ⌘ + 5 (Ctrl + 5 = Windows user). Note that you will have to either have the same rows selected or simply stand inside your comment block.

# =============================================================
# This
# is
# a
# multiline
# comment
# =============================================================

Jupyter

For Jupyter commenting can be done with the key combination:

Mac = Command + /

Windows = Ctrl + /


This short command works for both commenting and uncommenting. However, most international keyboard (non US keyboard) seem to struggle with this short command due to needing another key to do "/".



Learn more about Python here for all my posts: https://www.pls-fix-thx.com/python

If you have found this article or website helpful. Please show your support by visiting the shop below.


45 commenti


keeni crac
keeni crac
15 hours ago

Sigma-4pc.org is an excellent platform for anyone who loves to explore and download a variety of software programs. From productivity tools to advanced editing software, sigma-4pc.org provides a wide collection that caters to different user needs. The website ensures that all software is updated and safe to use, making it a reliable source for tech enthusiasts. By visiting sigma-4pc.org, you can save time searching for the right software elsewhere. This platform truly stands out because of its user-friendly interface and extensive library.

Mi piace

keeni crac
keeni crac
15 hours ago

Sigma-4pc.org is an excellent platform for anyone who loves to explore and download a variety of software programs. From productivity tools to advanced editing software, sigma-4pc.org provides a wide collection that caters to different user needs. The website ensures that all software is updated and safe to use, making it a reliable source for tech enthusiasts. By visiting sigma-4pc.org, you can save time searching for the right software elsewhere. This platform truly stands out because of its user-friendly interface and extensive library.

Mi piace


Xyz 178
Xyz 178
22 giu

Watch free HD film semi streaming @ Film 18+ ! No registration 100%

Mi piace

Steamunlocked offers a massive library of paid games available for free.

Mi piace
bottom of page