Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 21, 2022 06:22 am GMT

How to block a comment in Python?

Unlike other programming languages such as C, C++, and Java, Python does not support /*......*/ for multi-line comments or block comments, there is no built-in mechanism for block comments in Python. We have different conventions and syntax for block comment in Python. Here you will read about adding block comments to your code.

Table of contents

  1. Understanding the concept
  2. Using multiple single # line comments
  3. Using triple-quoted string literals for block comments
  4. Closing thoughts

Block comment in Python

Comments are specially marked lines of text in the program that are not evaluated. There are usually two ways to comment in any programming language.

The first is known as a single-line comment because it only applies to a single line in the "source code"(the program) and the second type of comment is a Block comment, and it usually relates to a paragraph of text. A block comment has a start and an end sign, and the computer ignores everything in between.

Using multiple single # line comments to add a block comment in Python

The most common way to comment out a block of code in Python is using the # character. Any line of code starting with # in Python is treated as a comment and gets ignored by the compiler.

Since only a line of code after the # character is considered a comment, so it is great for single-line comments, so you can use it as many times as you want in a single code.

Input:

# This is block comment# Made using # character# Used multiple timesprint ("Block Comment")

Output:

Block Comment

Using triple-quoted string literals to create block comment in Python

In order to write a proper block comment in Python, we can use triple-quoted syntax with multi-line strings. These strings should not be confused with Docstrings (triple-quoted string literals appearing right after a function/class/module to create documentation). If we use it to comment out multiple lines of code in Python, that block of code will be ignored, and only the lines outside the docstring will run.

Despite the fact that triple quoted string literals do not generate code, they are handled as such and must be indented properly within blocks in order to function properly!

Input:

"""This is block commentMade using # characterUsed multiple times"""print ("Block Comment") 

Output:

Block Comment

If you use 4 spaces (or a tab) for indentation, you will get an indentation error.

Closing thoughts

Since there's no built-in support for multi-line comments in Python, we can use a triple-quoted multi-line string for creating block comments. Still, you should generally stick to using regular Python comments using a # character, even if you have to use it for multiple lines. This is because docstrings are for documentation, and not for commenting out code. One can learn about more Python concepts here.


Original Link: https://dev.to/hrishikesh1990/how-to-block-a-comment-in-python-3ba6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To