4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to comment multiple lines in Python

Comment out multiple lines in Python with #, triple quotes, and editor shortcuts — when each approach fits best.
5 min readLast updated
python
'''
Multi line comment 
with docstring method
'''

Commenting one line in Python

Short answer: Python has no special syntax for multi-line comments, so you put # at the start of each line, as PEP 8 recommends for block comments. Most editors do it for you with Ctrl + / (or Cmd + / on a Mac) after selecting the lines. A triple-quoted string also hides text, but it is a string, not a real comment.

The standard way to comment a line of code on python will be with the # char.

python
#print("I won't be seen")
print("But I will!")
 
#Output-> But I will!

Commenting more than one line in Python using #

As you may already be thinking, if by using the #char we comment one line, we can spawn them to comment more lines? and the answer is yes! You can do just that.

python
#print("I won't be seen")
#print("If only I wouldn't have that numeral in front, I would be displayed")
print("But I will! I'm still the best")
#Output-> But I will! I'm still the best

Commenting multiple lines with docstring method in Python:

Docstring is the way to go if you want to comment a block of code on Python, since we are missing the /* commented text */ found on other languages as JavaScript, C#, etc.

python
'''
print("I won't be seen")
print("If you use the docstring method, I wont be displayed!")
'''
print("But I will! I'm still the best")
 
#Output-> But I will! I'm still the best

In docstring indentation is still important!

If you thought that you could escape indentation because you're commenting, you're wrong. Indentation is still pretty much needed to prevent errors.

python
def sumItGood(num1, num2):
"""
This function returns
the sum of 2 given numbers
"""
    return num1 + num2
print(sumItGood(2, 3))

This example will throw IndentationError: expected an indented block because we're not following the indentation rule.

If we apply the correct indentation to our code, then we'll receive the expected output of 5 and happily commented the code we wanted.

python
def sumItGood(num1, num2):
    """
    This function returns
    the sum of 2 given numbers
    """
    return num1 + num2
print(sumItGood(2, 3))
#Output-> 5

Commenting multiple lines in Python on different IDEs:

IDE makes our life as developers much easier with keyboard shortcuts and there's one that helps us to comment multiple lines with a single command

  • Visual Studio: Press Ctrl+K then Ctrl+C
  • Spyder IDE: Ctrl+1
  • IDLE: Alt+3
  • Jupyter Notebook: Ctrl+/
  • PyCharm: Ctrl+/

Note: These commands will place a # char on the beginning of all the lines selected to comment, will not insert a docstring.

You can check more of our articles at 4Geeks to learn more or solve issues with your coding. Hope you enjoy the reading and keep on the Geek side!

Commenting multiple lines in Python keeps your code readable for teammates and for your future self. A software development bootcamp builds habits like this through team projects and regular code reviews with mentors.

Frequently asked questions

What is the shortcut to comment multiple lines in VS Code?

Select the lines and press Ctrl + / on Windows and Linux or Cmd + / on macOS. VS Code adds a # at the start of each selected line, and pressing the same keys again removes it. The two-step shortcuts Ctrl + K then Ctrl + C to add comments, and Ctrl + K then Ctrl + U to remove them, also work.

Are triple quotes a real multi-line comment in Python?

No. Triple quotes create a string literal, not a comment. When that string is the first statement of a module, function or class, it becomes a docstring stored in __doc__ and shown by help(). Anywhere else, Python evaluates the string and discards it, but it still has to follow indentation rules. PEP 8 recommends # on each line for block comments.

How do you uncomment multiple lines in Python?

Select the commented lines and press the same toggle shortcut used to comment them: Ctrl + / in VS Code, PyCharm and Jupyter removes the leading # from every line. In IDLE, use Uncomment Region from the Format menu, which is Alt + 4, while Comment Region is Alt + 3. For a triple-quoted block, delete the opening and closing quotes.

Can you put a comment at the end of a line of code in Python?

Yes, that is an inline comment: everything after # on the line is ignored, as in total = price * qty # before tax. PEP 8 recommends separating it from the code by at least two spaces and starting it with # and a single space. A # inside a string is not a comment, so print("#1") prints #1.