free stats

Python Comments

Last Update : 02 Jul, 2022 Python, Programming

This is the Python fundamentals tutorial. In this tutorial, you will learn about Python comments. Also, You will learn about types of Python comments such as Single-Line comments, Multi-Line comments, Python Docstrings, etc.

Comments are descriptions that help programmers to understand the functionality of the program. Python interpreter is completely ignored when executing a program. Therefore, Comments have not affected the function of a code.

 

Advantages of Using Comments

There are many advantages to Programmer by using Python comments. Followings are the primary benefits of Python comments.

  1. The code becomes self-explanatory.
  2. Another programmer can easily understand the code.
  3. Help remember the usage of a function, method, or command in the code.
  4. Some blocks of code can ignore by the Python interpreter while testing.

 

Python Single-Line Comments

We use the hash symbol (#) in Python to write a single-line comment.

# Printing a Python string
print('Hello UX Python')

Here, the comment is:

# Printing a Python string

Python Interpreter ignores this comment line when executing code.

This is the output of a code:

Hello UX Python

Python interpreter ignores everything after the #. Therefore, We can write the above code as a single line.  Also, the output will be the same as the above code.

print('Hello UX Python') # Printing a Python string

 

Python Multi-Line Comments

There is no built-in mechanism for multi-line comments in Python like in other programming languages. However, You can prepend each line with a hash (#) to comment out multiple lines in Python.

# This is a
# multi-line comment

print("Hello UX Python")

Output :

Hello UX Python

 

Multi-Line Comments with String Literals

There is no specific way to write multi-line comments in Python language. However, the Python interpreter skips executing the string literals that are not assigned to a variable.

So, We can write single-line comments as follows :

# This is a single-line comment

'This is also a single-line comment'

The second line of the above code is a string. But it is not assigned to a variable. Therefore, the Python interpreter skips executing that string.

The quotation character of a string can be ' or ".

So, we can use multi-line Python strings to write multi-line comments.

'''

This is a Python
multi-line comment.

'''
print("Hello UX Python")

This multi-line string is also not assigned to a variable. Therefore, the Python interpreter skips executing that multi-line string.

 

Python Docstrings

A Python documentation string is a string used to document a Python class, module, method, or function.

We can write documentation strings as the first lines in a code block. Also, the documentation string is called a docstring.

So, We can use obj.__doc__ attribute to accessed Python docstring at run-time. Also, obj is the name of the function.

 

Summary

  • Use Python comments to document your code.
  • Use the hash symbol (#) to write a single-line comment.
  • Use Python docstrings to document classes, functions, and modules.

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.