In Python, you can comment multiple lines of code using multi-line strings or by using the
# character at the beginning of each line. Here are two common approaches:
- Using multi-line strings:
""" This is a comment line 1 This is a comment line 2 This is a comment line 3 """ print("This is not a comment")
In this approach, you enclose the comment lines within triple quotes (
"""). Python treats everything within the triple quotes as a multi-line string, which is effectively a comment. However, it’s important to note that this method doesn’t completely remove the code from execution; it is still parsed by the interpreter.
- Using the
# This is a comment line 1 # This is a comment line 2 # This is a comment line 3 print("This is not a comment")
In this approach, you prepend the
# character at the beginning of each line you want to comment out. Python interprets anything after the
# as a comment and ignores it during execution.
Both methods allow you to comment out multiple lines of code. The choice between them depends on personal preference and the context in which the code is being used.