In this lesson, We will learn about Python comments. Also, we will learn about the different types of Python comments and how to use them.
Comments are marked lines that make a code easier to comprehend and self-explanatory. The Python interpreter ignores all comments in the code. There are several methods to produce a comment depending on the type we want to add to our code.
The several comment types can be used in our Python program as follows.
Let’s talk about each of the remarks mentioned above kinds individually.
The # character denotes python comments on single lines. All characters beginning with the # character (and continuing till the end of the line) are considered part of the comment.
Example
#Single-line comment print ("Hello! World") print ("Hello! World")#Single-line comment
Output
Hello! World
Here, the comment is a Single-line comment.
The Python interpreter ignores this line, and the characters after the # are not used. Thus, you can write the above program in a single line too.
Unlike certain programming languages like C, Java, and others, Python does not have a dedicated capability for multiline comments. However, it does not follow that making multiline comments in Python is completely impossible.
There are two ways we can add comments to our Python code that span many lines. As shown below, you can comment on many lines:
We can utilize multiple single-line comments for a whole block. Each line of a comment on multiple lines can start with #, and a multiline comment is created using several # signs.
Example
#Python Block #A multiline comment #By implementing multi hash sign
In this case, every line is treated as a single comment and is ignored.
docstrings for Python
docstrings are the triple quotes that follow the definition of a function, method, or class (documentation strings). Docstrings are denoted by closing and opening quotations.
With the help of the __doc__ attribute, docstrings can be read and linked to objects. Visit Python docstrings to learn more.
The string literals can be used as a multiline comment and are also ignored by the Python interpreter. Many programmers utilize them as multiline comments in Python. Although it is OK to utilize docstrings to create multiline comments, it is crucial to remember that docstrings and comments serve quite different purposes. While docstrings, when used inside the Python function, can be viewed at run time, the Python Interpreter completely disregards comments in Python.
So, we can write a single-line comment using a string literal below
Example
‘It is also a comment using String Literals’
Similar to multiline comments, you can use multiline strings (triple quotes) to create comments. Here, we can see that the program’s statement, which is a string, is not assigned to a variable or function. Thus, the string is ignored by the interpreter.
Either’or “may be used as the quote mark.
Example
''' multiline comment! Hello World multiline comment! ''' print("Hello World")
Output
Hello World
The interpreter will not print all the statements in the above example enclosed by triple quotes. The interpreter ignores the multiline string because it isn’t set to a variable in this case and can be used as a multiline comment even though it isn’t officially one.