📌  相关文章
📜  r 之前的字符串 python 代码示例

📅  最后修改于: 2022-03-11 14:47:12.827000             🧑  作者: Mango

代码示例1
"""In the string, there is a special character: the escape character \, such as: \a Bell (BEL) \b backspace \t tab \r Carriage return (CR), move the current position to the beginning of this line \n Line feed (LF), move the current position to the beginning of the next line \\ represents a backslash \ \'represents a single quote ' \" represents a double quote " \? represents a question mark? In the string, it will be automatically escaped when it encounters the above character combination Adding r before a string in Python is equivalent to adding a \ before all \, which becomes \\, \\ is escaped to \, thus avoiding \ escaping n, t, r and other characters, and \ no longer Represents escape characters (escaping is prohibited)""" str1 ='\n' # n is escaped, representing a newline characterprint('str1:', str1, repr(str1), len(str1)) str2 = r'\n' # Add r, add another \ before \. According to the left-to-right operation rule, \\ escapes as \, n exists alone, and the final result is two characters: \ nprint('str2:', str2, repr(str2), len(str2), str2[0], str2[1])  # str2: \n '\\n' 2 \ n str3 ='\\n' # Equivalent to str2print('str3:', str3, repr(str3), len(str3), str3[0], str3[1])  # str3: \n '\\n' 2 \ n  # Thinking: Why do the following paths report errors? # Reason: \ is recognized as an escape character combined with \U to cause an errorpath1 = 'C:\Users\Administrator\Desktop\1.jpg' # Modify:path2 = 'C:\\Users\\Administrator\\Desktop\\1.jpg'path3 = r'C:\Users\Administrator\Desktop\1.jpg'