无法在 Python 中打印“\"(单反斜杠) [英] can't print '\' (single backslash) in Python
问题描述
我正在使用 Python 3 并试图找到一种方法来搜索将\"(单个反斜杠)插入到我的程序中的方法.
I am using Python 3 and am trying to find a ways to search for a way to insert a '\' (single backslash) into my program.
我遇到了这个错误:语法错误:扫描字符串文字时 EOL
I am gettin this error: SyntaxError: EOL while scanning string literal
推荐答案
你必须转义反斜杠:
\\
来自 Python 文档:
反斜杠 (\)
字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符.
The backslash
(\)
character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
此外,正如@Torxed 在他的回答中提到的,您可以使用前缀 r
或 R
:
Also, as @Torxed mentioned in his answer, you can use the prefix r
or R
:
字符串文字可以选择以字母 'r'
或 'R'
为前缀;此类字符串称为原始字符串,并使用不同的规则来解释反斜杠转义序列.
String literals may optionally be prefixed with a letter
'r'
or'R'
; such strings are called raw strings and use different rules for interpreting backslash escape sequences.
r"Some string with \ backslash"
这篇关于无法在 Python 中打印“\"(单反斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!