未终止的字符串文字 '\' [重复]

问题描述 投票:0回答:1

打印字符串时,出现语法错误:

SyntaxError: unterminated string literal at line (4)

我该如何解决这个问题?

print("      |``````````|  |~~~~")
print("         |    |     |     ~")
print("         |    |     |~~~~")
print("         |    |     | \")  # Syntax error in this line
print("         |    |     |  \ ")
print("         ~~~~~~     |   \")
python syntax-error string-literals
1个回答
5
投票

反斜杠在Python字符串中是特殊的。如果你想从字面上包含它们,你需要通过将它们加倍来转义它们:

print("      |``````````|  |~~~~")
print("         |    |     |     ~")
print("         |    |     |~~~~")
print("         |    |     | \\")
print("         |    |     |  \\ ")
print("         ~~~~~~     |   \\")
© www.soinside.com 2019 - 2024. All rights reserved.