Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between capital and lowercase string prefixes?

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

like image 893
bers Avatar asked Mar 19 '18 08:03

bers


People also ask

What is the difference between the upper and lower case string?

no difference. string is just synonym of String. Show activity on this post.

How do you check if there is a capital letter in a string?

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!

How do you check if a string is all lowercase?

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.

Why does English have upper and lowercase?

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.


2 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.

like image 125
Kos Avatar answered Sep 23 '22 11:09

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.

like image 32
Yann Vernier Avatar answered Sep 23 '22 11:09

Yann Vernier