2

I am struggling with multi line comments in Python, I understood I can use # at the start of every line of multi line comments but there is also another way of using """ at the start and end of the comment; however, in my interpreter the """ method gives an output instead of ignoring the comment.

>>> """this should
be a multi
line comment"""

And my interpreter gives the following output:

'this should\nbe a multi\nline comment'

Can someone explain it?

2 Answers 2

11

Triple quoted strings are used as comments by many developers but it is actually not a comment. It is similar to regular strings in python but it allows the string to be multi-line. You will find no official reference for triple quoted strings to be a comment.

In python, there is only one type of comment that starts with hash # and can contain only a single line of text.

According to PEP 257, it can however be used as a docstring, which is again not really a comment.

def foo():
    """
    Developer friendly text for describing the purpose of function
    Some test cases used by different unit testing libraries
    """
   <body of the function>
   

You can just assign them to a variable as you do with single quoted strings:

x = """a multi-line text
enclosed by
triple quotes
"""

Furthermore, if you try it in a repl, triple quoted strings get printed, had it really been a comment, should it have been printed?:

>>> #comment
>>> """triple quoted"""
'triple quoted'
Sign up to request clarification or add additional context in comments.

10 Comments

+1 for this - if you try it in a repl, triple quoted strings get printed, had it really been a comment, should it have been printed ?
@AryanBansal, even geeksforgeeks is not an official site to look at, furthermore, it also clearly mentions that Python does not provide the option for multiline comments. However, there are different ways through which we can write multiline comments. under the multiline comment section.
@AryanBansal, if you want, you can also take a look at PEP 8 style guide for comments
@AryanBansal I think I see your confusion. If you type 5 into the interpreter, it will output 5. If you type "a", it will output """a""". However, if you type """this should be ignored"""; x=5;print(x), it will ignore the string and just print 5, because it's not assigned to anything, and there is a command in the rest of the line. This is just an idiosyncrasy of i/o in an interactive shell. The same way you can do quick math in the shell, 5+6 will output 11 for example
@G. Anderson thanks for helping I think I understood now.
|
1

This is very easy to accomplish in python.

# This is a single-line comment

'''
This is a multi-line comment
'''

Just put the comments in ''' and put whatever you want inside of them!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.