Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
← Back to How to's
Edit on Github

How to comment multiple lines in Python

Written by:

1''' 2Multi line comment 3with docstring method 4'''

Commenting one line in Python

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

1#print("I won't be seen") 2print("But I will!") 3 4#Output-> Bit 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.

1#print("I won't be seen") 2#print("If only I wouldn't have that numeral in front, I would be displayed") 3print("But I will! I'm still the best") 4#Output-> Bit 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.

1''' 2print("I won't be seen") 3print("If you use the docstring method, I wont be displayed!") 4''' 5print("But I will! I'm still the best") 6 7#Output-> Bit I will! I'm still the best

In docstring indentation is still important!

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

1def sumItGood(num1, num2): 2""" 3This function returns 4the sum of 2 given numbers 5""" 6 return num1 + num2 7print(addNumbers(2, 3))

This example will throw IndentationError: expected an indented block becase we're not following the indentantion 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.

1def sumItGood(num1, num2): 2 """ 3 This function returns 4 the sum of 2 given numbers 5 """ 6 return num1 + num2 7print(addNumbers(2, 3)) 8#Output-> 5

Related: Why 4Geeks Academy teaches Python as a Backend Language

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+4
  • Jupyter Notebook: Ctrl+/
  • PyCharm: Ctrl+⇧ Shift+/

Note: These commands will place a # char on the beggining 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 you coding. Hope you enjoy the reading and keep on the Geek side!