Last Updated:

Single and multiple line comment in Python Code

Comments in a programming language are an explanation of a piece of code. This can be a description of a method, a class, or the purpose of a variable. Proper use of text inserts can improve the understanding of code by other developers when working on a common project. In addition, a text explanation will help to understand the written code and will speed up the search for an error.

Commenting on python code

 

Commenting is important in any project. This is an important part of the workflow for a developer. Without explaining what is written, the developer can easily get confused in the code. The explanation should be short and straightforward. At the same time, the main task of the footnote is to add new information.

Text insertions are ignored by the language interpreter because of a special character that is placed at the beginning of the string. Because of this sign, the note does not affect the execution of the program.

Types of comments

There are two kinds of text footnotes in a programming language:

  1. One-line,
  2. Multi-line.

You can make a callout in three ways: in a separate line, next to the operator, or as a multi-line block with explanatory text.

One-line comments

To write a one-line comment, you need to put a "#" symbol in front of the text. For example:

# print(«Hello World!»)

The interpreter will consider everything until the end of the line to be a comment. You can write the text both in English and in any other. The Style Design Guide recommends writing down no more than 80 characters per line. In practice, it is easier to read strings with no more than 70 characters.

Multi-line comments

Unfortunately, python does not have the ability to create multiline footnotes. However, there are several alternative ways:

  • Write a few one-line comments one after the other. This method is not the most convenient.
  • Enclose all footnote text in triple quotation marks. However, in this case, it will be a string that has not been assigned a variable. Therefore, this method is used for the explanatory design of the entire block.

However, if you place such a footnote after defining the method, Python will read it with a snippet of code associated with the function.

Single and multiple line comment in Python Code

Conclusion

Comments are used to explain your own code. This will allow other programmers to simplify the understanding of what is written.