A simple single line, triple-quoted comment is giving syntax error. Why?

Aditya Raj Bhatt adityarajbhatt at gmail.com
Wed Mar 18 13:46:20 EDT 2015


I always do single line comments with # but just for the sake of it I
tried it with ''' ''' and it gives me a syntax error.

In both the interpreter, and the source code text file, doing -

a = 5 '''a comment'''

results in a syntax error, with the very last quote at the end of the
line highlighted in red. Of course, if I do -

a = 5 #'''a comment'''

it works. I searched for the problem, and arrived at
http://stackoverflow.com/questions/397148/why-doesnt-python-have-multiline-comments
which says that there are no 'true' multiline comments in python and
that all those 'block' comments are actually triple-quoted strings. Then
I looked in the documentation and found
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
but it is a little bit too complex for my understanding (I'm just
starting python). 

So can someone tell me why a triple-quoted string gives a syntax error
if only in one line? Actually, there are other confusions I have too,
regarding using backslashes inside triple-quoted strings to form
multi-line comments, and a general uncertainty about triple-quoted
strings.

Can someone also provide a sort of a 'guide' to triple-quoted comments
in general?
Something like how I can just sum up index slices by saying in [a:b],
the 'counting' for a always starts with 0, a is included, everything up
to b but not b is included (assuming this is in fact the correct
explanation ;-))




More information about the Python-list mailing list