如何使用三重引号?

How to use triple quotes?

一些变量打印错误。

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

变量被打印成这样: image OS: Windows 10

解释器:Python IDLE 3.7.2

为您的字符串添加 r 前缀

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

这告诉 python 使用原始文字字符串,而不是使用反斜杠作为转义字符

问题是由于某些行末尾的反斜杠导致换行符不必要的转义。

只需在字符串前加上 r 前缀来声明原始字符串,即可解决问题。

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

错误是由于用于打印转义序列字符(\n、\t、\ 等)的字符串中的反斜杠字符引起的。使用原始字符串在屏幕上打印反斜杠字符,例如:

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

问题是因为您的某些行以 Python 转义字符结尾:\.

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

当一行以反斜杠结束时,它告诉 Python 该行在下一行继续。

if some_condition \
    or some_other_condition:

为了解决这个问题,您要么想要 'double up' 您的反斜杠来转义您的转义字符:

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

或者用 r 标记您的字符串以将其标记为 原始字符串

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\
| |       // |   | \
| |      //  | . |  \
'''