Piton

Python Multi-line Comments

Python Multi-line Comments

Every programming language provides a mechanism to add comments to projects. Comments are the simple lines in computer programs that are ignored by the compiler or interpreter. Comments are often written in natural language to increase programmer comprehensibility. Developers use comments to ignore some parts of the code in the debugging or testing phase.

Writing comments in Python can be very simple, and creating a comment in Python begins with the '#' symbol. This article explains how to create multi-line comments in Python.

Writing Multi-Line Comments in Python

Unlike many other programming languages, Python does not provide any specific way of writing multi-line comments. Nevertheless, there are other ways to cope with this issue.

In java, we write multiple comments as follows:

/*
Above, we are writing a multi-line comment.
This comment format does not work in Python.
*/

The above method of writing comments also does not work in Python.

Let us see some other ways to write multi-line comments in Python.

Using Multiple Symbol Line Comments

The '#' symbol is used for writing Python's single line comment. With consecutive multiple single-line comments, we can write multi-line comments. Let us see an example of this.

#Hello everyone
#this is a comment in Python
#i am writing multi-line comments
#using the single-line comment

This is one way of writing multiline comments. It is a bit of a time-consuming process, as it requires us to write out multiple single-line comments.

Now, let us see some other ways of writing multi-line comments in Python.

Using String Literals for Writing Multi-Line Comments

As discussed previously, Python does not provide an actual way for writing multi-line comments, but you can use string literals for writing multi-line comments. The Python interpreter overlooks literal strings not allocated to any variable and does not execute them. So, you can use unassigned string literals to write multi-line comments in Python. Let us look at an example of this.

'We are using the string literal as a comment'
'This is a multi-line comment'
'We are printing the hello world program'
print("Hello World")

Output

In the output, you can see that the Python interpreter does not output any error and prints the message 'Hello World.'

Using Triple Quoted String Literals for Writing Multi-Line Comments

Although triple quoted string literals are mainly used for writing docstrings, you can also use this tool to write multi-line comments. Be sure not to confuse the docstring with the triple quoted string literals that are used for writing multi-line comments. Incorrect indentation of triple quoted string literals will generate an error. Let us look at an example of this.

"
We are using the triple quoted string literals for multi-line comments
The programming language is Python
Let's print the hello world
"
print("Hello World")

Output

In the output, you can see that the Python interpreter does not output any error and prints the message 'Hello World.'

Now, let us use the triple quoted string literals inside a function for writing multi-line comments.

In the given example, we are printing the sum of two numbers.

def cal_sum():
"
we are calculating the sum of two numbers.
This is sum program
"
num1=10
num2=20
print("The sum is: ",num1+num2)
"
Python main function
"
def main():
cal_sum()
if __name__ == "__main__":
main()

Output

Now, let us change the indentation and in the output, you will see that the interpreter outputs an error.

def cal_sum():
"
We are calculating the sum of the two numbers.
This is sum program
"
num1=10
num2=20
print("The sum is: ",num1+num2)
"
Python main function
"
def main():
cal_sum()
if __name__ == "__main__":
main()

Output

The interpreter outputs the indentation error.

Conclusion

This article explains how to create multi-line comments in the Python programming language. Although Python does not provide any actual way of writing multi-line comments, you can address this issue by using the various methods discussed in this article.

Cum se folosește GameConqueror Cheat Engine în Linux
Articolul acoperă un ghid despre utilizarea sistemului de înșelăciune GameConqueror în Linux. Mulți utilizatori care joacă jocuri pe Windows folosesc ...
Cele mai bune emulatoare pentru console de jocuri pentru Linux
Acest articol va enumera programele populare de emulare a consolei de jocuri disponibile pentru Linux. Emularea este un strat de compatibilitate softw...
Best Linux Distros for Gaming in 2021
The Linux operating system has come a long way from its original, simple, server-based look. This OS has immensely improved in recent years and has no...