[Tutor] Trailing spaces affect output in a way I don't understand.

Steven D'Aprano steve at pearwood.info
Sun Dec 16 05:43:51 CET 2012


On 16/12/12 15:16, boB Stepp wrote:
> In the following code:
>
> print(
>          """
>
>
>           ______        ____        ___  ___    ______
>          / _____|      /    |      /   |/   |  |  ____|
>          | |          /  /| |     / /|   /| |  |  |__
>          | |   _     /  ___ |    / / |__/ | |  |   __|
>          | |__| |   /  /  | |   / /       | |  |  |___
>          \______/  /__/   |_|  /_/        |_|  |______|
>
>
>           ______    __      _    ______    ______
>          /   _  \  |  |    / /  |   ___|  |   _  \
>          |  | | |  |  |   / /   |  |__    |  |_| |
>          |  | | |  |  |  / /    |   __|   |   _  /
>          |  |_| |  |  | / /     |  |___   |  | \ \
>          \______/  |_____/      |______|  |__|  \_\
>
>
>
>          """
>
> )
>
> All trailing spaces have been trimmed off the end of the lines. It
> results in the following undesired output:
[...]

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

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.

* Change the string to a raw string, r"""...""" so that backslash
interpolation is turned off.




-- 
Steven


More information about the Tutor mailing list