[Tutor] Trailing spaces affect output in a way I don't understand.
boB Stepp
robertvstepp at gmail.com
Sun Dec 16 05:55:11 CET 2012
On Sat, Dec 15, 2012 at 10:43 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> What greatly puzzles me is that "GAME" prints correctly, but "OVER"
>> does not. Why?
>
>
> Wow! This is a tricky question, but so obvious in hindsight.
>
> The problem is that you have three lines, all in "OVER", that end with
> a backslash. In Python string literals, backslash-newline is interpreted
> as a line continuation, so that the next physical line is joined to the
> current line.
Ah, ha!! It is obvious now that you pointed it out! Many thanks! This
was driving me bonkers tonight.
> Two solutions are:
>
> * Add a space to the end of the backslashes. The space is invisible, and
> some editors may strip it out, so this is a fragile solution.
In my efforts to understand what was going on, I discovered that
PyScripter's (My current editor that I am playing around with.)
default setting is to trim all line-ending spaces, running into
exactly what you say.
> * Change the string to a raw string, r"""...""" so that backslash
> interpolation is turned off.
And I suppose I could also escape the backslash (\\), too. But your
suggestion sounds the best.
It is funny in retrospect: If I had wanted to create a newline
character I would do the correct thing, but seeing the backslashes as
part of a picture, even after I went into PyScripter's options and
turned on ALL special characters, I was still blind to the fact that a
\n was staring me in the face.
You are one sharp dude! As seem to be just about all of you...
Many thanks!
boB
More information about the Tutor
mailing list