This question already has an answer here:
这个问题在这里已有答案:
- What exactly do “u” and “r” string flags do, and what are raw string literals? 6 answers
- “u”和“r”字符串标志究竟做了什么,以及什么是原始字符串文字? 6个答案
I first saw it used in building regular expressions across multiple lines as a method argument to re.compile()
, so I assumed that r
stands for RegEx.
我第一次看到它用于构建跨多行的正则表达式作为re.compile()的方法参数,所以我假设r代表RegEx。
For example:
例如:
regex = re.compile(
r'^[A-Z]'
r'[A-Z0-9-]'
r'[A-Z]$', re.IGNORECASE
)
So what does r
mean in this case? Why do we need it?
那么在这种情况下r意味着什么呢?我们为什么需要它?
2 个解决方案
#1
111
The r
means that the string is to be treated as a raw string, which means all escape codes will be ignored.
r表示将字符串视为原始字符串,这意味着将忽略所有转义码。
For an example:
举个例子:
'\n'
will be treated as a newline character, while r'\n'
will be treated as the characters \
followed by n
.
'\ n'将被视为换行符,而r'\ n'将被视为字符\后跟n。
When an
'r'
or'R'
prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literalr"\n"
consists of two characters: a backslash and a lowercase'n'
. String quotes can be escaped with a backslash, but the backslash remains in the string; for example,r"\""
is a valid string literal consisting of two characters: a backslash and a double quote;r"\"
is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.当存在'r'或'R'前缀时,字符串中包含反斜杠后面的字符不会更改,并且所有反斜杠都保留在字符串中。例如,字符串文字r“\ n”由两个字符组成:反斜杠和小写“n”。字符串引号可以使用反斜杠进行转义,但反斜杠仍保留在字符串中;例如,r“\”“是一个有效的字符串文字,由两个字符组成:反斜杠和双引号; r”\“不是有效的字符串文字(即使原始字符串也不能以奇数个反斜杠结尾)。具体来说,原始字符串不能以单个反斜杠结尾(因为反斜杠会转义后面的引号字符)。另请注意,单行反斜杠后跟换行符被解释为字符串的一部分,而不是行继续。
Source: Python string literals
来源:Python字符串文字
#2
23
It means that escapes won’t be translated. For example:
这意味着转义不会被转换。例如:
r'\n'
is a string with a backslash followed by the letter n
. (Without the r
it would be a newline.)
是一个带反斜杠后跟字母n的字符串。 (没有r它将是换行符。)
b
does stand for byte-string and is used in Python 3, where strings are Unicode by default. In Python 2.x strings were byte-strings by default and you’d use u
to indicate Unicode.
b代表字节字符串,在Python 3中使用,其中字符串默认为Unicode。在Python 2.x中,默认情况下字符串是字节字符串,您可以使用u来表示Unicode。
#1
111
The r
means that the string is to be treated as a raw string, which means all escape codes will be ignored.
r表示将字符串视为原始字符串,这意味着将忽略所有转义码。
For an example:
举个例子:
'\n'
will be treated as a newline character, while r'\n'
will be treated as the characters \
followed by n
.
'\ n'将被视为换行符,而r'\ n'将被视为字符\后跟n。
When an
'r'
or'R'
prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literalr"\n"
consists of two characters: a backslash and a lowercase'n'
. String quotes can be escaped with a backslash, but the backslash remains in the string; for example,r"\""
is a valid string literal consisting of two characters: a backslash and a double quote;r"\"
is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.当存在'r'或'R'前缀时,字符串中包含反斜杠后面的字符不会更改,并且所有反斜杠都保留在字符串中。例如,字符串文字r“\ n”由两个字符组成:反斜杠和小写“n”。字符串引号可以使用反斜杠进行转义,但反斜杠仍保留在字符串中;例如,r“\”“是一个有效的字符串文字,由两个字符组成:反斜杠和双引号; r”\“不是有效的字符串文字(即使原始字符串也不能以奇数个反斜杠结尾)。具体来说,原始字符串不能以单个反斜杠结尾(因为反斜杠会转义后面的引号字符)。另请注意,单行反斜杠后跟换行符被解释为字符串的一部分,而不是行继续。
Source: Python string literals
来源:Python字符串文字
#2
23
It means that escapes won’t be translated. For example:
这意味着转义不会被转换。例如:
r'\n'
is a string with a backslash followed by the letter n
. (Without the r
it would be a newline.)
是一个带反斜杠后跟字母n的字符串。 (没有r它将是换行符。)
b
does stand for byte-string and is used in Python 3, where strings are Unicode by default. In Python 2.x strings were byte-strings by default and you’d use u
to indicate Unicode.
b代表字节字符串,在Python 3中使用,其中字符串默认为Unicode。在Python 2.x中,默认情况下字符串是字节字符串,您可以使用u来表示Unicode。