Home  >  Article  >  Backend Development  >  Detailed explanation of the use of left slash and right slash (forward slash and backslash) in Python

Detailed explanation of the use of left slash and right slash (forward slash and backslash) in Python

高洛峰
高洛峰Original
2017-03-28 15:52:043871browse

This article mainly introduces the relevant information about left slash and right slash (forward slash and backslash) in Python. It is very good and has reference value. Friends in need can refer to it

">

First of all, "/" slanted to the left is a forward slash, "\" slanted to the right is a backslash, which can be recorded as: The division sign is a forward slash. Generally speaking, for directory separators, Unix Use forward slashes / with Web, and backslashes with Windows, but now Windows

(1) The slashes in the directory

need to be entered when python reads files Directory parameters, list the following examples:

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 parameter in the open file function open() can be path or path1, path2, path3.

path: "\" is The special characters in the string become the original string after adding r, and the "\t" and "\r" in the string will not be escaped.

path1: The case does not affect the location of the file in Windows

path2: Use one "\" to cancel the special escaping effect of the second "\", which is "\\"

path3: You can also go to the corresponding directory by using a forward slash as the directory separator, and the path3 method in python also eliminates the trouble of backslash\escaping

(2) Slashes in regular expressions

The regular expression matches the backslash "\", why is it "\\\\" or r"\\"?

Because \ is a special symbol in regular expressions, in order to cancel its special meaning in regular expressions, you need to add a \ and it becomes \\, but the problem comes again, \ is also a special symbol in strings characters, so the special meaning of the two \ must be canceled respectively, which is \\\\. There is an original string operator in Python, which is used for those special characters in the string. In the original string, there is no conversion. This will cancel the escape function of \ in the string, that is, r"\\".

The above is the detailed content of Detailed explanation of the use of left slash and right slash (forward slash and backslash) in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn