Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to create multiline comments in Python?

I have recently started studying Python, but I couldn't find how to implement multi-line comments. Most languages have block comment symbols like

/*  */ 

I tried this in Python, but it throws an error, so this probably is not the correct way. Does Python actually have a multiline comment feature?

like image 741
Dungeon Hunter Avatar asked Oct 08 '11 12:10

Dungeon Hunter


People also ask

How do you create multiline comments in Python?

To write multiline comments in Python, prepend a # to each line to block comments. That means writing consecutive single-line comments. Start every line with the # sign consecutively, and you will achieve multiline comments.

How do you quick comment multiple lines in Python?

Shortcut to comment out multiple lines in Jupyter Notebook We can use ctrl+/ to comment out the selected lines of python code in Jupyter Notebook. This turns selected lines of code into comment as shown below. To uncomment the selected lines, we just have to again press ctrl+/ .

How do I make multiline comments?

You can comment multiple lines just by placing them between /* and */.

What symbol are used for Python multiline comments?

In some practices in Python, multiline comments are indicated using triple quotes (“ “ “ ” ” ”). In Python, even docstring is also defined in triple quotes, but docstrings are usually defined inside a class, function, module, or method as a first statement.


2 Answers

You can use triple-quoted strings. When they're not a docstring (the first thing in a class/function/module), they are ignored.

''' This is a multiline comment. ''' 

(Make sure to indent the leading ''' appropriately to avoid an IndentationError.)

Guido van Rossum (creator of Python) tweeted this as a "pro tip".

However, Python's style guide, PEP8, favors using consecutive single-line comments, like this:

# This is a multiline # comment. 

...and this is also what you'll find in many projects. Text editors usually have a shortcut to do this easily.

like image 141
Petr Viktorin Avatar answered Sep 29 '22 15:09

Petr Viktorin


Python does have a multiline string/comment syntax in the sense that unless used as docstrings, multiline strings generate no bytecode -- just like #-prepended comments. In effect, it acts exactly like a comment.

On the other hand, if you say this behavior must be documented in the official documentation to be a true comment syntax, then yes, you would be right to say it is not guaranteed as part of the language specification.

In any case, your text editor should also be able to easily comment-out a selected region (by placing a # in front of each line individually). If not, switch to a text editor that does.

Programming in Python without certain text editing features can be a painful experience. Finding the right editor (and knowing how to use it) can make a big difference in how the Python programming experience is perceived.

Not only should the text editor be able to comment-out selected regions, it should also be able to shift blocks of code to the left and right easily, and it should automatically place the cursor at the current indentation level when you press Enter. Code folding can also be useful.


To protect against link decay, here is the content of Guido van Rossum's tweet:

@BSUCSClub Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! :-)

like image 45
unutbu Avatar answered Sep 29 '22 17:09

unutbu