Creating Comments in Python
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Python was developed in the late 1980s, released in 1991, and is an interpreted, high-level and general-purpose programming language that emphasizes code readability. Python 3, released in 2008, is the current version.
Leaving informative comments on any code is important, as it helps others understand what the developer intended to do (or even reminds the developer themselves) and documents the code’s functionality. This guide will highlight how comments are left in python3 code.
Before You Begin
This guide assumes you have a Linode set up. If not, then familiarize yourself with our Getting Started guide to create and complete the basic installation steps for your Linode.
This guide assumes your Linode is running Python 3 or has a Python Virtual Environment installed. If not, then see our Python guides to find instructions for installing on your preferred Linux distribution.
Finally, this guide assumes you have a basic knowledge of Python and are comfortable editing using a text editor. If you are new to Python, then see the Python Software Foundation’s “Python for Beginners” guide for more information on what Python is, what it can do, and how to learn to use it.
Making Comments in Python
Python, like other languages, uses a special character or sequence of characters to indicate a comment. In Python’s case, the hash (#) character is used to note that the line is a comment (the Python parser later removes these lines).
One Line Comments
For example, a one-line comment would place the hash character and one character of white space (#
) at the start of a line and looks like this:
- File: comment.py
1 2 3 4
# I like my eggs with a side of Spam print('Eggs with a side of Spam.')
It is also convenient to use the hash character to “comment out” any code that may be going through testing or debugging:
- File: testing.py
1 2
# print('Eggs with a side of Spam.')
Inline Comments
Inline comments can be made but should be done cautiously. Inline comments will need to go after the code on a single line:
- File: inlinecomment.py
1 2
print('Eggs with a side of Spam.') # I like my eggs with a side of Spam
Multiline or Block Comments
The process for creating multiline comments is the same as a series of one-line comments stacked together, with each line of the comment starting with the hash character:
- File: multilinecomment.py
1 2 3 4 5
# I like my eggs with a side of Spam # I should also be courteous when asking for something print('I would like eggs with a side of Spam, please.')
Another Way to Make Multiline Comments
While not recommended, multiline comments can also be created using a delimiter that defines a text string constant:
- File: alternatemultilinecomment.py
1 2 3 4 5 6 7
""" I like my eggs with a side of Spam. I should also be courteous when asking for something. """ print('I would like eggs with a side of Spam, please.')
This is not the official or recommended way of handling multiline comments because, as it’s a string constant, it could cause issues with your code. It is mentioned in this guide only because it is possible to find code from others that have used it. It is recommended that you only use the official method.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on