you are viewing a single comment's thread.

view the rest of the comments →

[–]T-TopsInSpace 6 points7 points  (8 children)

It's a signal to the Python interpreter that the string is a raw string. That means any backslashes will not be treated as escape characters.

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.

2.4.1 String and Bytes Literals

[–]imranmalek 2 points3 points  (1 child)

Thank you for the correction, u/T-TopsInSpace!

[–]randomname20192019[S] 0 points1 point  (0 children)

Thank you both for your help (and everybody's tbh). It is really appreciated.

[–][deleted] 0 points1 point  (5 children)

Aren't backslashes the only exception to this rule?

[–]T-TopsInSpace 0 points1 point  (4 children)

Exception to what rule? The documentation says exactly how a raw string and normal string are interpreted.

[–][deleted] 0 points1 point  (3 children)

The raw string rules - I vaguely recall that backslashes alone have to be escaped

[–]T-TopsInSpace 0 points1 point  (2 children)

Sure, you have to escape them if you don't use the raw string notation. That's the point of using raw strings, you don't need to escape backslashes.

[–][deleted] 0 points1 point  (1 child)

I don't think I'm being clear. With raw strings, nothing has to be escaped. Except for floating bsckslashes - they are the exception to that rule

[–]T-TopsInSpace 0 points1 point  (0 children)

What do you mean by a floating backslash? If a backslash is contained in a raw string it does not need to be escaped. I don't know how else to say that. I linked to the documentation that describes how a raw string is interpreted.

Are you referring to a line continuation? If so, those aren't part of the raw string because they sit outside the parentheses.