Left Slash, right slash (forward slash and backslash) in Python _python

Source: Internet
Author: User
Tags in python

First, the "/" left tilt is a forward slash, "\" Right tilt is a backslash, can be written as follows: Division is a forward slash in general for directory separators, UNIX and web with forward slash/,windows with backslashes, but now windows

(i) the slashes in the catalogue

The directory parameters to be entered in the Python read file are listed in the following example:

Path = r "C:\Windows\temp\readme.txt"

path1 = r "C:\windows\temp\readme.txt"

path2 = "C:\\windows\\temp\\readme.txt"

Path3 = "C:/windows/temp/readme.txt"

The arguments in open () of the file function can be either path or path1, path2, Path3.

Path: "\" is a special character in a string, plus r changes to the original string, then the string escapes "\ t", "\ r"

Path1: Case does not affect Windows navigate to file

Path2: Use a "\" to cancel the special escape function of the second "\", namely "\"

Path3: Using a forward slash as a directory separator can also go to the corresponding directory, and in Python the way the PATH3 also eliminates the backslash \ escaped annoyance

(ii) the slashes in regular expressions

The regular expression matches the backslash "\", Why is "\\\\" or "R" or "\"?

Because in regular expressions \ is a special symbol, in order to cancel its special meaning in regular expressions need to add a \ to become a \, but the problem again, \ is also a special character in the string, so you have to two to cancel its special meaning, that is \\\\. Python has an original string operator for special characters in those strings that have no escape characters and cannot be printed in the original string. This allows you to cancel the escape function in the string, that is, r "\".

The above mentioned is a small set of Python to introduce the left Slash, right slash (forward slash and backslash), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.