Is there a difference between capital and lowercase string prefixes?

Question:

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"?

Asked By: bers

||

Answers:

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.

Answered By: Kos

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.

Answered By: Yann Vernier