Reputation: 185
I have created a constant which contains some ASCII art of a triangle, like this:
ascii_triangle =''' /\
/ \
/ \
/ \
/ \
/__________\ '''
I then use this as the argument for a function, so I can use the picture within a function (along with some other ASCII art and other arguments). The function isn't important, but the problem is that when I try to print it using
print(ascii_triangle)
within the function, the output looks like this:
/ / / / / /__________\
How can I make it look like a proper triangle?
Upvotes: 3
Views: 2198
Reputation: 403130
The ascii-art you posted has a trailing backslash just before each newline. This backslash if not followed by anything besides a newline is treated as the line continuation character.
Observe:
In [118]: x = 'foo \
...: bar \
...: baz'
In [119]: x
Out[119]: 'foo bar baz'
In the example above, there is nothing after the trailing backslash, so python believes it is a single string without any newlines. The same treatment is given even if you have a multiline quote for a string.
You'll need to add a space after the backslash, and make your string a raw literal.
In [123]: ascii_triangle =r''' /\
...: / \
...: / \
...: / \
...: / \
...: /__________\ '''
In [124]: print(ascii_triangle)
/\
/ \
/ \
/ \
/ \
/__________\
Try adding a space after the backslash in the first example, and you'd get
SyntaxError: EOL while scanning string literal
Indicating python will require a multiline quote for the string.
Upvotes: 5