Multiline comments in Python
If you are familiar with some other programming languages, like C/C++, or Java, you might be familiar that we have something called multiline comments. This just means that a comment, which can span more than one lines. Sometimes, it is necessary for us, to write some multiline comments, when we are required to write some detailed description, which is going to span more than one lines.
The thing is that in python, there is no such syntax for multiline comments, but if we are required to have some multiline comments in our python programs, then there are some things that we can do here. However, these are not the multiline comments, but we can make use of these ways. Let’s see what we can do to have multiline comments in python.
- In order to have comments in multiple lines, we can simply have multiple single line comments. Have a look at the below demonstration, which gives a big picture about using multiple single line comments here.
As you can see, in the above program, we have multiple single line comments, which are trying to describe something in multiple lines. So, one way to have multiline comments here in our python program is to have multiple single line comments.
- Another way to have comments in multiple lines, is to use the multiline strings in python. Basically, if you are creating a multiline string, and not assigning it to any variable, they will be ignored. So, we can use a multiline string, for having multiline comments in our python program.
Here is a demonstration for using the multiline strings as multiline comments in python programs.
As you can see in the above program, we have made use of the multiline string, as our multiline comments. Note that there is not much syntax for multiline comment in python, but these were some different ways that we discussed, like using multiple single line comments, or using the multiline string, without assigning it to some variable.
So, we have seen the need of comments in python, and how we can make use of comments in python. As and when require, we can make use of the comments in python. It is often a good practice to have comments in our programs to describe different things that we are doing. The basic thing is that it makes our code more readable, and understandable.