The r
before a string literal tells Python not to do any \
escaping on the string. For instance:
>>> print('a\nb')
a
b
>>> print(r'a\nb')
a\nb
>>>
The reason r
-prefixed strings are often used with regular expressions is because regular expressions often use a lot of \
's. For instance, to use a simple example, compare the regular expression '\\d+'
versus r'\d+'
. They're actually the same string, just represented in different ways. With the r
syntax, you don't have to escape the \
's that are used in the regular expression syntax. Now imagine having a lot of \
's in your regular expression; it's much cleaner to use the r
syntax.