Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiline comments
#1
Oneliner start with #

How to make comment block?
Reply
#2
Python does not have a separate syntax for multi line comments. You could use # for each line to be commented
Reply
#3
Multiline strings are commonly used to get around the limitation. The string definition needs to be properly indented. So,
"""
This is an example of a multi line comment
using a multi line string
"""
def foobar() :
    for idx in range (15) :
        print(idx)
    """
    Done with the loop
    What to do next?
    Probably just return
    """
    return

foobar()
This is also how docstrings are defined.
def fna(alpha, beta) :
    """
    This function does nothing useful.
    alpha should be a number
    beta should be a number
    fna returns the sum of the numbers
    """
    return alpha+beta

print(fna.__doc__)
Output:
This function does nothing useful. alpha should be a number beta should be a number fna returns the sum of the numbers
Reply
#4
Also help() works if have docstrings in functions or classes.
>>> help(fna)
Help on function fna in module __main__:

fna(alpha, beta)
    This function does nothing useful.
    alpha should be a number
    beta should be a number
    fna returns the sum of the numbers
Reply
#5
Comment block can be done like this :
"""
This is a comment.
This is another comment.
This is the last comment.
"""

# The above lines were a comment block, whereas this is only a one-line comment
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#6
With how syntax highlighting works I prefer using # for all comments and """ for doc strings and multi-line strings. But I hardly ever use multi-line comments that I don't want to be part of the documentation.
Reply
#7
OK Thank you everyone for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Algorithm for extracting comments from Python source code Pavel1982 6 529 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  capturing multiline output for number of parameters jss 3 820 Sep-01-2023, 05:42 PM
Last Post: jss
  How do I add comments from a text-file to an array of folders? clausneergaard 2 1,794 Feb-08-2023, 07:45 PM
Last Post: Larz60+
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,191 Jul-20-2022, 09:05 AM
Last Post: arbiel
  Delete multiple comments with a single API call (facebook) Ascalon 0 2,319 Dec-04-2021, 08:33 PM
Last Post: Ascalon
  Presenting multiline data into single line aaronbuhu 1 1,806 Aug-05-2021, 10:57 AM
Last Post: jamesaarr
  Can the comments produce errors in python? newbieAuggie2019 9 4,284 Nov-26-2019, 12:19 AM
Last Post: micseydel
  How can I create a multiline input in ipython? DataMower 3 4,383 Oct-28-2019, 08:50 PM
Last Post: DataMower
  Variable comments on Visual Studio Code sal 2 3,215 Oct-19-2019, 02:13 PM
Last Post: sal
  Change values in string multiline DavidFernandez 4 3,094 Aug-26-2018, 08:09 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020