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.
Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.
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.
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).
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
# print('Eggs with a side of Spam.')
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
print('Eggs with a side of Spam.') # I like my eggs with a side of Spam
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.')
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.
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