In this tutorial, you’ll learn how to use Python to create multiline comments. While Python itself doesn’t support multiline comments, there are two options that let you span your comments over multiple lines. You learn how Python comments work and how to keep them going across many lines. You’ll also learn how to use multiline strings to create multi-line comments.
Many programming languages offer multi-line comments. Some of them, for example will use a /* comment */
notation that looks like this:
/*
This is a multi
line comment in
JavaScript
*/
Python, unfortunately, doesn’t offer this type of comment. But you do have options to let your comments go across multiple lines.
The Quick Answer: Use # to Create Multiline Comments in Python
Table of Contents
Creating Python Comments
Python provides a single option to create a comment, using the pound sign, sometimes called the hash symbol, #
.
Comments in programming languages are used to explain code and provide context. They often help make your code more readable by explaining the why of your code (i.e., why you are writing your code the way you are). Sometimes comments are also used to prevent code from running, and this is often referred to as “commenting code out”.
You have a number of different options of where you place the pound symbol:
- If you place the
#
symbol at the beginning of a line, the entire line becomes a comment - If you place the
#
somewhere along the line of code, then everything that follows it becomes a comment
Let’s take a look at a few examples:
# Creating Comments in Python
# The single hashtag is used to create a single-line comment in Python
a = 3 # You can use them in-line, to explain what you're doing
# Placing them at the front, though, makes the whole line a comment
In the next section, you’ll learn how to use the single-line comment to create a multiline comment in Python.
Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.
Python Multiline Comments with Consecutive Single-Line Comments
While Python doesn’t support multiline comments, you can use single-line comments to make your comments span multiple lines. What we can do to create a comment that goes across multiple lines is simply to comment out multiple lines.
Let’s see what this looks like in Python:
# This is a comment that starts on one line
# and keeps going on another
# and maybe even one more
This is actually the approach that’s recommended by Python’s PEP-8 style guide. It also represents the only “real” way to create comments in Python. The approach that follows this section isn’t “really” a comment but rather a workaround.
While it may seem really overkill to have to comment out multiple lines. Many code editors like VS Code provide keyboard shortcuts to do this. For example, in VS Code, you can simply use CTRL+/
or CMD+/
to comment out a line.
Learning how to use these keyboard shortcuts can make easy work of something that feels tedious. Because of this, this operation becomes almost second nature and this makes it easier to live with the lack of official multi-line comments.
In the next section, you’ll learn how to use Python multiline strings not assigned to variables to emulate multiline comments in Python.
Want to learn how to use the Python zip()
function to iterate over two lists? This tutorial teaches you exactly what the zip()
function does and shows you some creative ways to use the function.
Python Multiline Comments with Multiline Strings
A simple way that we can emulate multiline comments in Python is to use multiline strings. The way that we can create strings that span multiple lines is to use triple quotation marks, either with single quotes or double quotes.
Let’s take a look at how we can Python to create strings that span multiple lines:
a_multi_line_strin = '''
this is a string
that spans multiple
lines'''
another_multi_line_string = """
this is another
string that spans
multiple lines"""
An interesting string about Python strings is that the Python interpreter ignores any strings that aren’t assigned to variables, meaning that they are treated indirectly as comments.
Aren’t these Python docstrings?
You may notice that these triple quote look very similar to Python docstrings. Docstrings are used to document a function and are created using triple quotes.
When you do not create triple quotes indented as the first line of a function, they are treated as simple strings.
Let’s see what the difference looks like in Python
def some_function():
"""This is a docstrinfg
to explain a function"""
def another_function():
"""This docstring will throw an error
since it's not indented properly."""
some_string = """This is a string that's assigned
to a variable and spans multiple lines."""
"""Because this string
isn't assigned to a variable,
it will be treated as a multi-line
comment and will be ignored."""
You’ll notice four different scenarios above:
- A proper docstring, with correct indentation and following a function definition
- An improperly indented docstring that will raise an error
- A multi-line function assigned to a variable
- An unassigned multi-line string that behaves like a comment
Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!
Conclusion
In this tutorial, you learned how to use Python to create multi-line comments. While Python itself doesn’t have multiline comments, you are able to create them using two different methods. You learned how to use Python to span single line comments over multiple lines and how to use multiline strings to create comments.
Comments are incredibly helpful tools in programming. They allow us to document code, providing the rationalization as to why we do things. They also allow us to provide explanations of how complex pieces of code may work. They also serve an unintended purpose of allowing us to “comment out” certain sections of our code. This means to take code and turn it into comments so that it will not run when the program is run.
To learn more about comments in Python, check out the official documentation here.
Pingback: Functions in Python • datagy