Introduction to RawString in Python, pythonrawstring

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.