30

Visual Studio Code highlights string literals with prefixes r and R differently:

Match = re.search(r"\d{2} \d{4} \d{2}:\d{2}:\d{2})", Output) 
Match = re.search(R"\d{2} \d(4} \d{2}:\d{2}:\d{2})", Output) 

enter image description here

Is there a difference in meaning between these two notations? Are different conventions used for r and R? What about other prefixes like "b", "u", or "f"?

3
  • 2
    According to Python docs, none whatsoever
    – Martin
    Mar 19, 2018 at 8:34
  • Reverting my edit was rude, I took the time to convert your image to searchable text. If you wanted to add the image back you could have done so in a subsequent edit. Apr 13, 2018 at 14:58
  • @StefanCrain sorry, first time. Fixed.
    – bers
    Apr 15, 2018 at 6:25

2 Answers 2

47

There's no difference in meaning between these notations. Reference:

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

The same goes for other prefixes.


Now regarding VSCode behaviour:

  • the first coloring (with yellow {2}) happens when the editor assumes you're writing a regular expression,
  • the second one (with blue {2}) happens when the editor thinks you're writing a format string, something like "{0}, {1}!".format("Hello", "world").

This becomes more obvious when we add some more syntax:

highlighted code snippet

Now, looks like VSCode should treat R"literal" the same as r"literal", but instead it colors it the same as "literal", which is probably a tiny bug that nobody spotted because everyone writes lowercase r.

Correction from comment: It's not a bug, it's a feature! VSCode's highlighter makes clever use of the fact that r and R prefixes are equivalent, and allows you, the developer, to have correct coloring by adopting a convention of using r for regex raw strings and R for non-regex raw strings.

Raw strings are often interpreted as regular expressions. This is a bit of a problem, because depending on the application this may actually not be the most common case. (...) MagicPython follows a convention that a lower-case r prefix means a regexp string, but an upper-case R prefix means just a raw string with no special regexp semantics.

6
  • Thanks! So if coloring of R... is a bug, is it reasonable to assume that coloring of r"C:\Adam\Betty\Peter\Tom" (with a yellow P and T) is another bug?
    – bers
    Mar 19, 2018 at 8:46
  • 2
    More of a misfeature. That's VSCode thinking the raw string must be a regular expression, where \A and \B are special sequences, but \P and \T are not. Mar 19, 2018 at 8:49
  • 9
    @bers: It’s not really possible in general to tell whether a given raw string is supposed to represent a regex or a path or anything else. VS Code appears to be assuming that’s a regex, either because it does so for all raw strings or because it’s making a bad guess.
    – Ry-
    Mar 19, 2018 at 8:49
  • 2
    They are using MagicPython for syntax highlighting. MagicPython follows a convention that a lower-case r prefix means a regexp string, but an upper-case R prefix means just a raw string with no special regexp semantics. github.com/MagicStack/MagicPython#strings
    – voidvector
    Feb 13, 2019 at 23:28
  • 1
    @voidvector thanks so much! I'm glad to learn this is documented behaviour - I added this to the answer.
    – Kos
    Feb 20, 2019 at 10:30
4

In general, Python is case sensitive. Per the string literal syntax specification, however, string prefixes can be either case (or order). So the difference is visual, although tradition is mostly to use lower case, and upper case letters can be harder to distinguish.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.