• 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 / Single Line and Multi Line Comments in Python

Single Line and Multi Line Comments in Python

Author: Aditya Raj
Last Updated: April 7, 2021

A comment is a piece of code that isn’t executed by the compiler or interpreter when the program is executed. Comments can only be read when we have access to the source code. Comments are used to explain the source code and to make the code more readable and understandable. In this article, we will see how to write single line and multi line comments using different methods in python.

What is a single line comment in python?

Single line comments are those comments which are written without giving a line break or newline in python. A python comment is written by initializing the text of comment with a # and terminates when the end of line is encountered. The following example shows a single line comment in a program where a function is defined to add a number and its square to a python dictionary as key value pair.


#This is a single line comment in python
def add_square_to_dict(x,mydict):
    a=x*x
    mydict[str(x)]=a
    return mydict

We can also add a single line comment after another statement.

#This is a single line comment in python
print("Pythonforbeginners.com") #This is also a python comment

What is a multi line comment?

As the name specifies, a multi line comment expands up to multiple lines. But python does not have syntax for multi line comments. We can implement multi line comments in python using single line comments or triple quoted python strings.

How to implement multi line comments using # sign?

To implement multi line comments using # sign, we can simply depict each line of a multi line comment as a single line comment. Then we can start each line by using # symbol and we can implement multi line comments.

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

When writing multi line comments using # symbol, we can also start multi line comments after any python statement.

#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 implement multi line comments using triple quoted strings?

Multi line strings in python can be used as multi line comments if they are not assigned to variables. When the string isn’t assigned to any variable, they are parsed and evaluated by the interpreter but no byte code is generated because no address can be assigned to the strings. Effectively the unassigned multi line string works as a multi line comment.  

"""This is 
a 
multiline comment in python
which expands to many lines"""

Here we have to keep in mind that the multi line comments are only string constants that have not been assigned to any variable. So they have to be properly intended unlike single line comments with # symbol so that syntax errors can be avoided.

Also, multi line comments which use triple quotes should always start with a newline which is not the case for a single line comment. 


#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"""

Conclusion

In this article, we have seen how to write single line and multi line comments in python. We have also seen how to write multi line comments using strings. 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 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 Pyspark 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 Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

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