1

From documentation:

The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

Types also match; type(u"text") == type(ur"text"), and same goes when you remove u. Therefore, I have to ask: what is the difference between these two? If there is no difference, why use r at all?

MatthewRock
  • 1,071
  • 1
  • 14
  • 30

1 Answers1

3

For example:

>>> len(ur"tex\t")
5
>>> len(u"tex\t")
4

Without r, the \t is one character (the tab) so the string has length 4.

Use r if you want to build a regular expression that involves \. In an non-r string, you'd have to escape these which is not funny.

>>> len(u"\\")
1
>>> len(ur"\\")
2
  • To add to this: 'r' changes how the Python parser interprets the string, i.e. handling escape sequences. It does not result in a different type as with 'u' or 'b'. – fmarc Jun 01 '16 at 12:32