How to Do Block Comment in Python

Block comments, also known as multiline comments are great when you want to add a paragraph of information to your code.

These descriptions or explanations are helpful, especially if you or someone else will be reading the code in the future. In other languages like Javascript or C++, block comments are quite common but in Python, there is actually no specific syntax for block comments.

However, there is a widely accepted style of block commenting. Today, we will talk about how to do block comments in Python.

Step 1: Understanding Python Comments

Before moving on to the block comment, let’s take a quick look at how Python handles comments. Python uses the # for comments. Anything on the same line that comes after # will not be read or executed by the Python interpreter.

In the example above, the comment won’t affect the code functionality, which is to print “Hello, World!” onto the console.

Step 2: How to Use Block Comment in Python

Again, Python does not have an explicit syntax for block comments. The accepted practice is to use multiple single-line comments (consisting of the # symbol) for block comments.

Step 3: An Alternate Approach – The Triple Quotes

An alternate approach for block comments in Python is using triple quotes (“”” “”” or ”’ ”’). This is more associated with multi-line strings or docstrings but can serve the purpose, especially inside functions or classes.

Although technically recognized as a string by the Python interpreter, this multiline string block adds the ability to add a descriptive paragraph in code without execution.

Step 4: Using IDEs or Code Editors for Block Comment

Most modern code editors or Integrated Development Environment (IDE) offer shortcuts to create block comments. Here’s how to do it in popular editors:

IDE or EditorShortcut
PyCharmCtrl + /
VSCodeCtrl + /
Jupyter NotebookCmd + / for Mac or Ctrl + M for Windows

Please note that all the shortcuts mentioned above are for creating multiple single-line comments, which is our first way to block comments in Python.

Full Code:

Output

Hello, World!
Block comment example.

Conclusion

In Python, there isn’t an actual syntax for block comments. Instead, users can utilize single-line comments in succession, or for a more descriptive approach use triple quotes.

Being able to add effective comments can make your code more readable and maintainable which is crucial when working on large-scale projects.