How to Comment in Python

To comment in Python, you can use the # symbol for single-line comments or triple quotes """ for multi-line comments.

The following examples show how to comment in Python using these two methods.

Using # Symbol

We can use the # symbol to add single-line comments in Python.

Suppose we have the following code:

# This is a comment in Python
print("Hello, World!") # This is also a comment

Output: 👇️

Hello, World!

In this example, we use the # symbol to add single-line comments. The text following the # symbol is ignored by the Python interpreter.

Using """ Triple Quotes

We can use triple quotes """ to add multi-line comments in Python.

Suppose we have the following code:

"""
This is a multi-line
comment in Python.
It can span multiple lines.
"""
print("Hello, World!")

Output: 👇️

Hello, World!

In this example, we use triple quotes """ to add a multi-line comment. The text enclosed within the triple quotes is ignored by the Python interpreter.

Conclusion

We can use the # symbol for single-line comments and triple quotes """ for multi-line comments in Python. These methods provide a convenient way to add comments to your Python code.