Multi-line Comments In Python

Friday, April 13, 2012 - 18:27

Python doesn't officially support multi-line comments, but there is a way of implementing the same functionality using an existing language construct. Single line comments in Python are written like this:

1
# This is a single line comment.

You can create a multi-line comment in Python by using a multi-line string. Unless it is part of a docstring at the start of a class, function or module then it is ignored.

1
2
3
4
5
'''
This is
a multiline
comment
'''

Multi-line strings are used when you want to create a string that is split across several lines. If you create one and don't assign it to a variable then it is just thrown away. Multi-line strings can be used in the following way.

1
2
3
4
5
6
multilinestring = '''
This is
a multiline
string
'''
print multilinestring
Category: 
Tags: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

No, it is not ignored. It is parsed.
see https://stackoverflow.com/questions/7696924/way-to-create-multiline-comments-in-python

Add new comment