What is Multiline Comment in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Comments are considered to be an important part of any code. Comments are useful information used by developers to make a reader more knowledgeable about the source code. Usually, it includes the details of the logic the code follows, in other words, how the code works and will execute.

It is often cited as a valuable programming convention that does not impact the program's output but greatly improves its readability. Unlike most programming languages, Python does not support multiline comments. Even though Python does not support multiline comments, it is practiced to declare them with triple quotes.

As you will learn Python in-depth, you'll get to know that it is important to see how the alignment is done; the indentation is used instead of curly braces to define a function. Thus, we should pay attention to indentation when writing the comments. You can add a note before the function body but within the function, also known as a docstring, and similar to multiline comments that do not have any indents.

The following methods can help you achieve it.

Ways to Achieve Multiline Comments in Python

Consecutive Single-Line Comment

The # character indicates a comment in Python code. To comment on a line, place a hash character before it. Multiline comments can be written in Python using consecutive single-line comments. Here's an example:

Output

The interpreter will ignore the comments on execution. So any context starting with # will be ignored and not considered by the interpreter. Use this Online Compiler to compile your Python code.

Using Multi-Line String as a Comment

A Python multiline comment consists of a group of text enclosed in a delimiter (""") at each end. To avoid syntax errors, make sure to indent the first """ correctly. These are used when the comment content fits into multiple lines.

Output

Take your Python knowledge to the next level with our Python course with certification. Sign up today and master Python!

Docstrings

Python comes with an inbuilt feature called DocString(“documentation strings”) that allows you to associate documentation with modules, functions, classes, and methods. The multiline comments become docstrings if they are placed directly after a function or class signature. Due to docstring, which is a corollary of the multiline comment in Python, multiline comments are not supported since the compiler considers them as they are described within functions, classes, and modules. Strings with multiple lines may be used as multiline comments; unless used as docstrings, they will not generate code.

Conclusion

  • Comments in Python or any other language are written to make the code easily understandable.
  • The compiler and interpreter ignore the comments in a program, so they do not run.
  • It is preferred to use # even for multiline comments.
  • If you wish to create something similar to multiline comments in Python, you might consider using triple-quote """ strings, but this isn’t a perfect option, and your "comments" might turn into docstrings by accident
  • Docstrings are intended to be documentation rather than commenting out code.

Read More: