Is there a way to create multiline comments in Python?

Dungeon Hunter picture Dungeon Hunter · Oct 8, 2011 · Viewed 1.6M times · Source

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?

Answer

Petr Viktorin picture Petr Viktorin · Oct 8, 2011

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, and this is also what you'll find in many projects. Text editors usually have a shortcut to do this easily.