Frage

Hello good people of Stack Overflow,

It seems that I am back here with an inquiry.

I have 'borrowed' some ascii art off the internet to use in my program, don't worry, I have given full credit to the artist at the end of it. Anyway, no matter how I format it, it is distorting where letters and symbols are placed.

For instance, When the code is this:

'''
 ___________.._______  
| .__________))______|
| | / /      ||
| |/ /       ||
| | /        ||.-''.
| |/         |/  _  \
| |          ||  `/,|
| |          (\\`_.'
| |         .-`--'.
| |        /Y . . Y\
| |       // |   | \\
| |      //  | . |  \\
| |     (')  |   |  (`)
| |          ||'||
| |          || ||
| |          || ||
| |          || ||
| |         / | | \
""""""""""|_`-' `-' |"""|
|"|"""""""\ \       '"|"|
| |        \ \        | |
: :         \ \       : :  
. .          `'       . .
'''

(it's part of a list)

When running, it appears to be this:

 ___________.._______
| .__________))______|
| | / /      ||
| |/ /       ||
| | /        ||.-''.
| |/         |/  _  | |          ||  `/,|
| |          (\`_.'
| |         .-`--'.
| |        /Y . . Y| |       // |   | \
| |      //  | . |  \
| |     (')  |   |  (`)
| |          ||'||
| |          || ||
| |          || ||
| |          || ||
| |         / | | """"""""""|_`-' `-' |"""|
|"|"""""""\ \       '"|"|
| |        \ \        | |
: :         \ \       : :  
. .          `'       . .

My question is, is it something I may have done, or is it a bug...

Thanks in advance! John. Let me quickly clear a few things up, what I was asking was why it was being printed wrongly in the program output... Sorry for any confusion caused. The reason given in the answer marked, was correct, and I ammended my program to fix the error by changing the end of each line, replacing the backslash with a different character.

War es hilfreich?

Lösung

The problem is that \ is the escaping character. For instance, you may have seen "\n" used to mean a newline and "\t" to mean a tab character. Here you mostly have "\" which means a single literal '\' character, and "\" followed by an actual newline which omits that newline in the produced string.

The simplest solution is to just replace every "\" in your string with "\":

>>> print "//\\\\"
//\\

However, this would of course make it harder to view and edit the ASCII art.

A nice solution for your case is to use raw strings by using an "r" prefix on the string literal:

>>> print r"""/\
... \/"""
/\
\/
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top