+ 2
âBHâY
It is used often when creatinf regex patterns.
Because regular expressions mostly consist of escape charactees.
Some Examples:
\w ---> word/char
\d ---> digit
\s ---> space
More here:
https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/python_regex.asp
We need to use r-prefix to escape and preserve backslash that is needed for processing regex in the background program.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Another use of r-prefix or ignoring escape sequence is for opening files.
Sometimes, backslash is used to separate each location/directory.
file = open("...\user\folder\text.txt")
However, this will escape the \t in \text.txt. Therefore we should avoid escaping sequence by using r-prefix or escaping the backslash themselves.
file = open("...\\user\\folder\\text.txt")
OR
file = open(r"...\user\folder\text.txt")
0
r-strings are actually normal strings where every '\' is automatically substituted with '\\'
So there is no difference
0
R-strings is very helpful for regex.
https://docs.python.org/3/howto/regex.html#the-backslash-plague