How do we create multiline comments in Python?
Last Updated :
01 Aug, 2022
Comments are pieces of information present in the middle of code that allows a developer to explain his work to other developers. They make the code more readable and hence easier to debug.
Inline Comment
An inline comment is a single line comment and is on the same line as a statement. They are created by putting a '#' symbol before the text.
Syntax:
# This is a single line comment
Example:
Python3
def my_fun():
# prints Geeksforgeeks on the console
print("GeeksforGeeks")
# function call
my_fun()
Output:
GeeksforGeeks
Note: Although not necessary, according to Python there should be a single space between # symbol and comment text and at least 2 spaces between comment and the statement.
Block Comment
Block comments in Python usually refer to the code following them and are intended to the same level as that code. Each line of block comment starts with a '#' symbol.
Syntax:
# This is a block comment
# Each line of a block comment is intended to the same level
Example:
Python3
def my_fun(i):
# prints GFG on the console until i
# is greater than zero dercrements i
# by one on each iteration
while i > 0:
print("GFG")
i = i-1
# calling my_fun
# it will print GFG 5 times on the console
my_fun(5)
Output:
GFG
GFG
GFG
GFG
GFG
Docstrings
The documentation string is string literal that occurs as the first statement in a module, function, class, or method definition. They explain what can be achieved by the piece of code but should not contain information about the logic behind the code. Docstrings become __doc__ special attribute of that object which makes them accessible as a runtime object attribute. They can be written in two ways:
1. One-line docstring:
Syntax:
"""This is a one-line docstring."""
or
'''This is one-line docstring.'''
Example:
Python3
def my_fun():
"""Greets the user."""
print("Hello Geek!")
# function call
my_fun()
# help function on my_fun
help(my_fun)
Output:
Hello Geek!
Help on function my_fun in module __main__:
my_fun()
Greets the user.
Note that for one-line docstring closing quotes are on the same line as opening quotes.
2. Multi-line docstring:
Syntax:
"""This is a multi-line docstring.
The first line of a multi-line docstring consist of a summary.
It is followed by one or more elaborate description.
"""
Example:
Python3
def my_fun(user):
"""Greets the user
Keyword arguments:
user -- name of user
"""
print("Hello", user+"!")
# function call
my_fun("Geek")
# help function on my_fun
help(my_fun)
Output:
Hello Geek!
Help on function my_fun in module __main__:
my_fun(user)
Greets the user
Keyword arguments:
user -- name of user
The closing quotes must be on a line by themselves whereas opening quotes can be on the same line as the summary line.
Some best practices for writing docstrings:
- Use triple double-quotes for the sake of consistency.
- No extra spaces before or after docstring.
- Unlike comments, it should end with a period.
Similar Reads
Multiline Comments in Python A multiline comment in Python is a comment that spans multiple lines, used to provide detailed explanations, disable large sections of code, or improve code readability. Python does not have a dedicated syntax for multiline comments, but developers typically use one of the following approaches:It he
4 min read
How to write Comments in Python3? Comments are text notes added to the program to provide explanatory information about the source code. They are used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also help the later generation for understanding and mai
4 min read
Interesting Fact about Python Multi-line Comments Multi-line comments(comments block) are used for description of large text of code or comment out chunks of code at the time of debugging application.Does Python Support Multi-line Comments(like c/c++...)? Actually in many online tutorial and website you will find that multiline_comments are availab
3 min read
How to create a multiline entry with Tkinter? Tkinter is a library in Python for developing GUI. It provides various widgets for developing GUI(Graphical User Interface). The Entry widget in tkinter helps to take user input, but it collects the input limited to a single line of text. Therefore, to create a Multiline entry text in tkinter there
3 min read
How to Add New Line in Dictionary in Python Dictionaries are key-value stores that do not inherently support formatting like new lines within their structure. However, when dealing with strings as dictionary values or when outputting the dictionary in a specific way, we can introduce new lines effectively. Let's explore various methods to add
3 min read
Multiline String in Python A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double
4 min read
Read a file line by line in Python Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). In this article, we are going to study reading line by line from a file.Example:Pythonwith open('file
4 min read
Proper Indentation for Multiline Strings in Python In Python, indentation plays a crucial role in code readability and structure, especially with multiline strings. Multiline strings, defined using triple quotes (""" or '''), allow for strings that span multiple lines, preserving the formatting within the quotes. Proper indentation of these strings
3 min read
PYGLET â Inserting Text in Formatted Document In this article, we will see how we can insert text in the formatted document in the PYGLET module in python. Pyglet is easy to use but a powerful library for developing visually rich GUI applications like games, multimedia, etc. A window is a "heavyweight" object occupying operating system resource
2 min read
Break a long line into multiple lines in Python Break a long line into multiple lines, in Python, is very important sometime for enhancing the readability of the code. Writing a really long line in a single line makes code appear less clean and there are chances one may confuse it to be complex. Example: Breaking a long line of Python code into m
4 min read