Python - How to comment a block of code
In this tutorial, we are going to learn about how to comment a block of code in Python with the help of examples.
In Python, we can add a single line comment by using a #
character followed by the comment.
Here is an example:
# this is a single line comment
Commenting a block of code
Python does’t provide any built-in syntax to comment a multiple lines unlike other programming languages.
To comment a block of code in python, add the #
character infront of each line.
Example:
# price = 20
# print(price)
# name = 'raj'
or We can use the triple quotes """
syntax in Python.
The triple quotes syntax is normally used to create a multiline strings, but the string literals are ignored that are not assigned to a variable, so we are using it to add a multiline comment.
Here is an example:
"""
price = 20
print(price)
name = 'raj'
"""
Note: The multiline comment starts after the """
and ends before the """
.
Any code or text, we add between """
and """
is ignored by the python compiler
Conclusion
Python doesn’t have a built-in way to comment a block of code like other programming languages, to add a block comment use #
character infront of every line.