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

Thomas 'PointedEars' Lahn PointedEars at web.de
Wed Mar 18 16:21:48 EDT 2015


Aditya Raj Bhatt wrote:

> 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. […]
> 
> So can someone tell me why a triple-quoted string gives a syntax error
> if only in one line?

As with all syntax errors in all programming languages, the code cannot be 
produced by the grammar (here: 
<https://docs.python.org/3/reference/grammar.html>, assuming Python 3.x).

> 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?

Do not use them as comments in this way.  See also my other follow-up.

[x] done

> 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 ;-))

It is correct:

<https://docs.python.org/3/tutorial/introduction.html#strings>
<https://docs.python.org/3/tutorial/introduction.html#lists>
<https://docs.python.org/3/tutorial/datastructures.html#more-on-lists>
<https://docs.python.org/3/library/stdtypes.html#typesseq>

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list