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

Denis McMahon denismfmcmahon at gmail.com
Wed Mar 18 20:53:43 EDT 2015


On Wed, 18 Mar 2015 10:46:20 -0700, 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.

> ...

> So can someone tell me why a triple-quoted string gives a syntax error
> if only in one line?

A triple quoted string is a multiline string literal. A string literal 
(of any sort) is a basic python expression.

An expression is often, but by no means exclusively, part of an 
assignment statement. However, an expression may also exist just as a 
simple expression.

There is nothing special about a triple quoted string that makes it a 
comment, other than it is sometimes used as such in it's guise as a basic 
expression.

However, you can't have multiple expressions on a line without some sort 
of operand or separator between them.

a = 5 '''text'''

is just as wrong as:

q = 4,5,6   [3,5,7,9]

or

k = 6-2  {56:91, 'fred': 'peter'}

or even

m = 62.3   56.7   101.2

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list