[Python-ideas] multiline string notation

Mike Meyer mwm-keyword-python.b4bdba at mired.org
Tue Sep 28 10:58:42 CEST 2010


On Tue, 28 Sep 2010 10:27:07 +0200
spir <denis.spir at gmail.com> wrote:

> Hello,
> 
> 
> 
> multiline string
> 
> By recently studying a game scripting language (*), and designing a toy language of mine, I realised the following 2 facts, that may be relevant for python as well:
> 
> 
> 
> -1- no need for a separate multiline string notation
> 
> A single string format can deal text including newlines, without any syntactic or parsing (**) issue: a string notation just ends with the second quote.
> No idea why python introduced that distinction (and would like to know it); possibly for historic reason? The only advantage of """...""" seems to be that this format allows literal quotes in strings; am I right on this?

No, you're not. The ' form allows literal "'s, and vice versa. The
reason for the triple-quoted string is to allow simple multi-line
string literals.

The reason you want both single and multi-line string literals is so
the parser can properly flag the error line when you forget to
terminate the far more common single-line literal. Not as important
now that nearly everything does syntax coloring, but still a nice
feature.

> -2- trimming of indentation
> 
> On my computer, calling the following function:
>     def write():
>         if True:
>             print """To be or not to be,
>             that is the question."""
> results in the following output:
>     |To be or not to be,
>     |        that is the question.
> This is certainly not the programmer's intent. To get what is expected, one should write instead:
>     def write():
>         if True:
>             print """To be or not to be,
>     that is the question."""
> ...which distorts the visual presentation of code by breaking correct indentation.
> To have a multiline text written on multiple lines and preserve indentation, one needs to use more complicated forms like:
>     def write():
>         if True:
>             print "To be or not to be,\n" + \
>             "that is the question."
> (Actually, the '+' can be here omitted, but this fact is not commonly known.)

And in 3.x, where print is a function instead of a statement, it could
be (leaving off the optional "+"):

def write():
  if True:
     print("To be or not to be,\n"
           "that is the question.")

So -1 for this idea.

       <mike


-- 
Mike Meyer <mwm at mired.org>		http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org



More information about the Python-ideas mailing list