Introduction to RawString in Python, pythonrawstring
Summary
1. ''': it can represent "multi-line comments", "multi-line strings", and "The single double quotation marks in it are not escaped"
2. r indicates raw
3. r only applies to the backslash (pay attention to the single \ problem)
What is the use of raw string? Raw string automatically escapes the backslash.
>>> print('\n')>>> print(r'\n')\n>>>
(Note: two empty rows are displayed because print () automatically adds an empty row)
Another example:
>>> r'\\\\\\' == '\\\\\\\\\\\\'True>>> print('\\\\\\\\\\\\')\\\\\\>>> print(r'\\\\\\')\\\\\\>>>
The above is the basic function of raw string.
The so-called
print(r'''1234''')
And
print('''1234''')
The reason for the same effect is that
There is no \ in the three quotes, so the r hero is useless.
One thing to note is that raw string does not allow print (R' \ ') to work. During compilation, Python will still try to use a backslash to escape single quotes, resulting in no termination of the string.
Example:
>>> Print (r 'C: \ Windows \ system32 ')
C: \ Windows \ System32
>>> print('C:\\Windows\\System32')C:\Windows\System32>>> print('C:\Windows\System32')C:\Windows\System32>>>
The last line can also take effect because \ W and \ S are nothing. Therefore, in this example, Python finds that "cannot be escaped", so it directly prints the escape character without any escaping. However:
>>> print('C:\Windows\System32\new')C:\Windows\System32ew>>> print(r'C:\Windows\System32\new')C:\Windows\System32\new
It's different.
The above introduction to RawString in Python is all the content shared by the editor. I hope you can give us a reference and support the help house.