0

I am making an text based rpg/adventure game and i want to add some ASCII art. I have used triple quotation marks for a multiline string like so:

    print("""
                   /\                       /\                        /\                       /\
                  /**\                     /**\                      /**\                     /**\
                 /****\   /\      /\      /****\   /\               /****\   /\      /\      /****\   /\
                /      \ /**\    /  \    /      \ /**\             /      \ /**\    /  \    /      \ /**\
               /  /\    /    \  /    \  /  /\    /    \    /\     /  /\    /    \  /    \  /  /\    /    \
              /  /  \  /      \/      \/  /  \  /      \  /  \   /  /  \  /      \/      \/  /  \  /      \
             /  /    \/ /\     \      /  /    \/ /\     \/     \/  /    \/ /\     \      /  /    \/ /\     \
            /  /      \/  \/\   \    /  /      \/  \/\   \     /  /      \/  \/\   \    /  /      \/  \/\   \
         __/__/_______/___/__\___\__/__/_______/___/__\___\___/__/_______/___/__\___\__/__/_______/___/__\___\_
    """)

Ideally, this is what it should look like when i run the program.

However, it looks like this instead which is not ideal at all and looks extremely messy:

enter image description here

I was wondering if anyone could point me in the right direction on how to go about this problem. Thanks in advance!

some_user_3
  • 403
  • 1
  • 5
  • 17
  • I reopened your question to add an answer explaining the specific problem in your code, and the reason why it happens - the duplicate you chose was about an unnecessary encoding and didn't really address this. – Thierry Lathuille Feb 10 '21 at 10:32

3 Answers3

1

You should raw your string, as follows:

#     v
print(r"""
                   /\                       /\                        /\                       /\
                  /**\                     /**\                      /**\                     /**\
                 /****\   /\      /\      /****\   /\               /****\   /\      /\      /****\   /\
                /      \ /**\    /  \    /      \ /**\             /      \ /**\    /  \    /      \ /**\
               /  /\    /    \  /    \  /  /\    /    \    /\     /  /\    /    \  /    \  /  /\    /    \
              /  /  \  /      \/      \/  /  \  /      \  /  \   /  /  \  /      \/      \/  /  \  /      \
             /  /    \/ /\     \      /  /    \/ /\     \/     \/  /    \/ /\     \      /  /    \/ /\     \
            /  /      \/  \/\   \    /  /      \/  \/\   \     /  /      \/  \/\   \    /  /      \/  \/\   \
         __/__/_______/___/__\___\__/__/_______/___/__\___\___/__/_______/___/__\___\__/__/_______/___/__\___\_
    """)

This is because the backslash "\" is a special character in Python string.

keepAlive
  • 6,369
  • 5
  • 24
  • 39
  • 1
    no that solves the problem! thanks! i was just wondering what ```repr``` does. Other than that, i would accept it but Stackoverflow wants me to wait 10 minutes. – some_user_3 Feb 10 '21 at 10:10
  • Actually, the simplest way to think about `repr` is that it allows you to render strings as you see them. – keepAlive Feb 10 '21 at 10:12
  • 1
    oh ok that makes sense. so like raw text? – some_user_3 Feb 10 '21 at 10:13
  • That's it @some – keepAlive Feb 10 '21 at 10:13
  • 1
    cool. thanks for the help! i will keep that in mind for future use – some_user_3 Feb 10 '21 at 10:14
  • 1
    @some_user_3 Yep, for [docstrings](https://www.programiz.com/python-programming/docstrings) (when writing some LaTeX) for example, it is extremely useful, etc.. – keepAlive Feb 10 '21 at 10:15
  • 2
    The `r` stands for "raw", making it a "raw string literal"; it is completely unrelated to the `repr` function. ([docs](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals)) – kaya3 Feb 10 '21 at 10:24
  • Thx kaya3, I learnt something today, indeed *cf.* *[this](https://stackoverflow.com/questions/13669682/in-context-of-python-raw-string/13669799)* or *[that](https://stackoverflow.com/questions/57791178/raw-strings-and-repr-are-equivalent)*. @some keep kaya3's comment in mind (!) – keepAlive Feb 10 '21 at 10:30
1

In addition to the other answers, another thing that can mess up the ASCII art is indention, for example:

print("""these 3 lines
         should start
         in the same place""")

Will output:

these 3 lines
         should start
         in the same place

To fix this you need to do:

print("""these 3 lines
should start
in the same place""")

But it still wouldn't work if the print statement is indented.

for i in range(1):
    print("""these 3 lines
    should start
    in the same place""")

Will output:

these 3 lines
    should start
    in the same place

To fix this your string should always start in the begining of the line, regardless of indent, like so:

for i in range(1):
    print("""these 3 lines
should start
in the same place""")

But that's ugly.

Roy Cohen
  • 1,540
  • 1
  • 5
  • 22
0

The main problem in your code is that the \ at the end of the lines is interpreted as a line continuation, so the next line just gets concatenated to the previous one.

You can avoid this by adding any character at the end of the line, after the backslash, like a space for example (as the line continuation is triggered only if the backslash is the last character in the line).

Another solution is to use a raw string, starting with r""" ..... As stated in the documentation:

Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the literal, not as a line continuation. (emphasis mine)

This will also prevent any potential escape sequence starting with a backslash to be interpreted as such.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50