• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Comments / How to create multiline comments in Python

How to create multiline comments in Python

Author: Aditya Raj
Last Updated: May 13, 2021

Comments are used to enhance readability and understandability of source code by providing proper information about the code. Comments are not executed by an interpreter or compiler and they are used in the code only for assistance of the programmer. In this article, we will understand how to create multiline comments in python. 

What is a multiline comment in python?

As we can see in the name itself, a multiline comment is a python comment which expands to multiple lines i.e. multiline comments are those comments which expand to two or more lines in the source code. Theoretically speaking, there is no syntax for multiline comments in python but we can implement multi line comment using single line comment or triple quoted strings. We will look at both methods to implement multiline comments one by one.

How to create multiline comments in python using # symbol?

As we know that single line comment in python are implemented by adding # symbol before the comment and it terminates whenever a line break occurs, we can put a # symbol at the start of each line of a multiline comment. In this way we can implement multi line comments using Single Line comments.This can be seen as follows. The function in the example adds a number and its square to a python dictionary as key-value pair.

#This is a multiline comment in python
#and expands to more than one line 

def add_square_to_dict(x,mydict):
    #This is an example of block of code where the block consists of an entire function
    a=x*x
    mydict[str(x)]=a
    return mydict

We can start a single line comment in python after any statement in the source code. In the similar way when we are implementing multiline comments using # symbol, we can start a multi line comment after any statement in the code.

#This is a multiline comment in python
#and expands to more than one line
print("Pythonforbeginners.com") #This is also a python comment
#and it also expands to more than one line.

How to create multiline comments in python using multiline strings?

To use multiline strings as multiline comments in python, we can use a multi line string without assigning it to any variable. In this way, when the interpreter will execute the code, no byte code will be generated for the unassigned string  as no address can be assigned to it. In this way, a multiline string will act as a multiline comment. This can be seen as follows.

"""This is 
a 
multiline comment in python
which expands to many lines"""
print("PythonforBeginners.com")

We can start a multiline comment after a statement in python when we implement multi line comments with # symbol. But we cannot start multi line comments after any statement if we are using multiline strings to implement multiline comments . If done, it will cause error. This can be seen as follows.


#This is a multiline comment in python
#and expands to more than one line
"""This is 
a 
multiline comment in python
which expands to many lines"""
print("Pythonforbeginners.com") """This is not 
a 
multiline comment in python
and will cause syntax error"""

When we implement comments using # symbol, we do not have to care about indentation but when we use multiline strings as comments, we have to follow proper indentation otherwise errors will occur. This can be seen as follows.

            #This is a multiline comment in python
#and expands to more than one line wthout caring for indentation
"""This is 
a 
multiline comment in python
which expands to many lines and should follow indentation"""
print("Pythonforbeginners.com")

Conclusion

In this article, we have seen the various ways to create multiline comments in python. We also saw how to use multiline comments so that they do not cause error in the program. Stay tuned for more informative articles.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Comments Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us