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

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Python Basics
    • Comments
    • Functions
    • Lists
    • Loops
    • Strings
    • Syntax Basics
  • Code Examples
  • Categories
    • Basics
    • Code Snippets
    • Python On The Web
    • Scripts
    • System & OS
    • Dictionary
    • Modules In Python
    • Lists
    • Modules
    • OS


You are here: Home / Basics / Strings / How to write comments in Python

How to write comments in Python

Last Updated: April 6, 2021

Comments in python are parts of source code which aren’t executed by the python interpreter. Comments contribute no functionality to the application program and have no meaning for users of the application program. But comments are of great help for the programmers. Comments increase the readability and understandability of the source code and help the programmers in refactoring or debugging the code and while adding source code for new features in the application program. In this article, we will see how to write comments in python using different ways.

How to write single line comments in Python?

We can write a single line python comment using # symbol. In python, anything written after # till a line break including the symbol itself is considered as a comment. Whenever a line break is encountered, the single line comment terminates.We can start a single line comment anywhere in the program using # symbol and whole statement after the symbol becomes a comment.

Following is an example code in which a single line comment is  written inside a python dictionary.

myDict={"name":"PythonForBeginners.com",
        #This is a single line comment inside a python dictionary
        "acronym":"PFB"
        }
print(myDict["acronym"])

We can also start a single line comment after a python statement.


myDict={"name":"PythonForBeginners.com",
        #This is a single line comment inside a python dictionary
        "acronym":"PFB"
        }
print(myDict["acronym"]) #This is single line comment which starts after a python statement

How to write multi line comments in Python?

Theoretically, python doesn’t have a syntax for multi line comments but we can use other ways like multi line strings and single line comments  to write multi line comments in python.

We can write multi line comments in python using single line comment by writing single line comments on consecutive lines as shown in the following example.

#This is multiline comment
#written using single line comments
#for a demonstration
myDict={"name":"PythonForBeginners.com",
        "acronym":"PFB"
        }
print(myDict["acronym"]) 

We can also start multi line comments in python after a python statement while implementing multi line comments with # symbol because it necessarily behaves as a single line comment.

#This is multiline comment
#written using single line comments
#for a demonstration
myDict={"name":"PythonForBeginners.com",
        "acronym":"PFB"
        }
print(myDict["acronym"]) #This is multiline comment after a python statement
#written using single line comments
#for a demonstration

To write multi line comments in python, we can also use multi line strings. If we do not assign a multi line string to any variable, the multi line string will be parsed and evaluated by the interpreter but no byte code will be generated for the multi line string because no address can be assigned to them. Effectively, the multi line strings will work as multi line comments. Following is an example of multi line comment written using multi line strings.

"""This is multiline comment
written using multi line strings
for a demonstration"""
myDict={"name":"PythonForBeginners.com",
        "acronym":"PFB"
        }
print(myDict["acronym"])

We have to keep in mind that multi line comments when written using multi line strings are not true comments and they are just unassigned string constants. For this reason, they should follow proper indentation and can be started only from a newline. If we try to write a multi line comment using a multi line string after any python statement, it will cause syntax error. 

myDict={"name":"PythonForBeginners.com",
        "acronym":"PFB"
        }
print(myDict["acronym"])"""This is not a multiline comment and 
will cause syntax error in the program"""

Conclusion

In this article, we have seen how to write comments in python using # symbol and multiline strings. Stay tuned for more articles.

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Strings Date Originally Published: March 31, 2021

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Control Flow crawler Data Types Development Dictionary Dictionary Data Structure In Python Errorhandling Error Handling Exceptions Fabric Files ftplib Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pil pip Python Python On The Web Python Strings Requests Scraping Scripts sh Strings System & OS urllib2 Web

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Iterating over dictionary in Python
  • Difference between comments and docstrings in Python
  • Shortcut to comment out multiple lines in Python
  • Convert a list containing float numbers to string in Python
  • Single Line and Multi Line Comments in Python

Python Courses

  • Datacamp: Intro To Python
  • 2021 Complete Python Bootcamp
  • Python Mega Course: Build 10 Real World Apps
  • Python Data Science Bootcamp
  • Complete Python Developer: Zero to Mastery

Copyright © 2012–2021 · PythonForBeginners.com

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