在Python中,`r`或`R`是原始字符串常量的标志,意味着字符串中的所有字符都会按照字面意义来解释,不会进行转义。通常,在字符串中,反斜杠`\`是一个转义字符,它用来引入特殊字符序列,如换行`
`、制表符`\t`等。但是,如果你在字符串前加上`r`或`R`,比如`r"string"`或`R"string"`,那么字符串中的`\`就不会被当作转义字符,而是作为普通字符。
例如:
普通字符串,其中的\n表示换行
s1 = "this is a string\nnew line"
print(s1) 输出:this is a string
new line
原始字符串,其中的\n表示字面上的反斜杠和n
s2 = r"this is a string\nnew line"
print(s2) 输出:this is a string\nnew line
原始字符串常量在处理正则表达式和包含特殊字符的系统路径时特别有用