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)
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"
?
no difference. string is just synonym of String. Show activity on this post.
To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!
The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.
The terms “uppercase" and “lowercase" come from the way in which print shops were organized hundreds of years ago. Individual pieces of metal type were kept in boxes called cases. The smaller letters, which were used most often, were kept in a lower case that was easier to reach.
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:
{2}
) happens when the editor assumes you're writing a regular expression,{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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With