The backslash (\) character is used to escape characters that
otherwise have a special meaning, such as newline, backslash itself,
or the quote character. 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.
...
Unless an 'r' or 'R' prefix is present, escape sequences in strings
are interpreted according to rules similar to those used by Standard C
它被称为原始字符串文字。
根据^{} :
在Python中,它只意味着原始字符串。意思是字符串中的任何内容都是字符串。例如,如果要添加斜杠:
您需要在斜杠前面添加
\
,因为斜杠是转义字符。例如,\n
表示新行。如果保持字符串原始,它将确保字符串中的内容不会以特殊方式解释。例如,如果您编写了
string2 = r"\n"
,它只会在字符串时给您"\n"
,而不是一个新行。您可以从here了解更多关于它们的信息。
现在,这在您的例子中完成了,因为文件路径有很多斜杠,我们希望避免使用这么多的反斜杠。
r'..'
字符串修饰符使'..'
字符串被逐字解释为。这意味着,r'My\Path\Without\Escaping'
将计算为'My\Path\Without\Escaping'
-而不会导致反斜杠转义字符。previor相当于'My\\Path\\Without\\Escaping'
字符串,但没有raw修饰符。注意:字符串不能以奇数个反斜杠结尾,即
r'Bad\String\Example\'
不是正确的字符串。相关问题 更多 >
编程相关推荐