Lesson 3
Discovering your Paths
Chapter 10: Commenting
Multi-line comments
“But writing #
in front of every line is tedious! I have a LOT to say!”
If you really need it, you can also use triple quotes (either """
or '''
) to write comments that span multiple lines, without having to put a #
in front of every line.
'''
You can write long, multi-line comments.
This can span multiple lines.
Just close your comments with triple quotes.
'''
A twist!!
And now, here comes the twist! These multi-line comments are actually NOT true comments.
These are actually multi-line str
literals (\n
represents a “new line” character in the code below).
"""
works too.
>>> lyrics = '''Never gonna give you up,
... Never gonna let you down,
... Never gonna run around and desert you.
... '''
>>> lyrics
'Never gonna give you up,\nNever gonna let you down, \nNever gonna run around and desert you.\n'
>>> print(lyrics)
Never gonna give you up,
Never gonna let you down,
Never gonna run around and desert you.
If you remember from Lesson 2, Python does not output anything if you only provide an expression, such as a string. It will still create the string object, but then it does not do anything with the string like assigning it to a variable, for example.
So string literals can technically be used as comments since Python will read them but will not do anything with them.
'''
I am exploiting the fact that Python will not do anything
to me or reveal any of my juicy secrets.
Which is why I can type all I want and nobody running my
code will see me.
Multi-line comments for the win!
'''
"Technically, this is also not revealed to the world."
print("This is what I show to the public.")
Such multi-line string comments are conventionally used to document your own custom functions (we will discuss this when the time comes).
For now, use #
for your comments instead of multi-line strings to avoid your code being hard to read.