This article describes the usage of raw strings in Python and is shared with you for your reference. The details are as follows:
The origin of Python's original strings is precisely because of the existence of regular expressions. The reason is the conflict between ASCII characters and regular expression special characters. For example, the special symbol "\b" represents the backspace key in the ASCII character, but at the same time "\b" is also a special symbol of a regular expression, representing "match a word boundary".
In order for the RE compiler to treat the two characters "\b" as the string you want to express, instead of a backspace key, you need to escape it with another backslash, that is, you can write: "\\b".
But doing so will complicate the problem, especially when your regular expression string has a lot of special characters, it is easier to be confusing. Generally speaking, raw strings are often used to simplify the complexity of regular expressions.
In fact, many Python programmers only use raw strings when defining regular expressions.
The following examples are used to illustrate the difference between the backspace key "\b" and the regular expression "\b" (with or without the original string):
...
>>> m = ('\\bblow', 'blow') # escaped \, now it works # After escaped with \, it now matches
>>> if m is not None: ()
...
'blow'
>>> m = (r'\bblow', 'blow') # use raw string instead #Use original string >>> if m is not None: ()
...
'blow'
You may have noticed that we use "\d" in regular expressions without using the original string, and there is no problem. That's because there are no corresponding special characters in ASCII, so the regular expression compiler can know that you are referring to a decimal number.
This feature of original strings makes some work very convenient, such as the creation of regular expressions. Regular expressions are strings that define advanced search matching methods. They are usually composed of special symbols representing characters, groupings, matching information, variable names, and character classes. The regular expression module already contains enough symbols. But when you have to insert extra symbols to make special characters behave like ordinary characters, you are trapped in the quagmire of "character numbers"! The original string will come in handy.
Apart from the original string symbol (the letter "r" before the quotes), the original string has almost the same syntax as the normal string. The 'r' can be lowercase or uppercase, the only requirement is that it must be close to the first quote. In the first example of the 3 examples, we need a backslash plus a "n" instead of a newline character.
'\n'
>>> print '\n'
>>> r'\n'
'\\n'
>>> print r'\n'
\n
In the next example, we can't open our README file anymore. Why? Because '\t' and '\r' are treated as special symbols that are not in our filename, but they are actually 4 independent characters in the file path.
File "", line 1, in ?
f = open('C:\windows\temp\', 'r')IOError: [Errno 2] No such file or directory: 'C:\\win- dows\\temp\'
>>> f = open(r'C:\windows\temp\', 'r')>>> ()
'Table of Contents (please check timestamps for last update!)\n'
>>> ()
I hope this article will be helpful to everyone's Python programming.