LavaTime
LavaTime

Reputation: 75

How to use triple quotes?

some variables are being printed wrong.

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

the variable is being printed like that: image OS: Windows 10

interpreter: Python IDLE 3.7.2

Upvotes: 0

Views: 1391

Answers (4)

Merig
Merig

Reputation: 2011

Prefix an r to your string

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

This tells python to use the string a raw literal, not using backslashes as escape characters

Upvotes: 3

alxwrd
alxwrd

Reputation: 2470

The problem is because some of your lines end with the Python escape character: \.

The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

String and Bytes literals

When a line ends with a backslash, it tells Python that the line is continued on the next line.

if some_condition \
    or some_other_condition:

To counter this, you either want to 'double up' your backslashes to escape your escape characters:

gv_sixTries = '''
| |        /Y . . Y\\
| |       // |   | \\\\
| |      //  | . |  \\\\
'''

Or mark your string with an r to mark it as a raw string.

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters.

String and Bytes literals

gv_sixTries = r'''
| |        /Y . . Y\
| |       // |   | \\
| |      //  | . |  \\
'''

Upvotes: 0

windstorm
windstorm

Reputation: 385

The error comes due to a backslash character in the string which is used for printing escape sequence characters(\n, \t, \, etc.). Use raw string for printing a backslash character on the screen, something like:

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

Upvotes: 0

Tryph
Tryph

Reputation: 6209

The problem comes from unwanted escaping of new line characters due to antislashes at the end of some lines.

Simply declare a raw string by prefixing your string by r, and it will fix the problem.

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

Upvotes: 0

Related Questions