1r means the string will be treated as raw string.
2When an 'r' or 'R' prefix is present, a character following a backslash
3is included in the string without change, and all backslashes are left inside
4the string
1# a Rstring is a string that treat backslashs as normals caracters
2#Exemple:
3#normal string
4>>> print("This is a line feed : \n and it's usefull!")
5This is a line feed :
6and it's usefull!
7# R-string
8>>> print(r"This is not a line feed /n :(")
9This is not a line feed /n :(
10
11# It's mostly used to write Paths
12# Exemple
13my_path = "C:\Users\Me\Desktop\MyFile.txt" #Don't works a all but
14my_path = r"C:\Users\Me\Desktop\MyFile.txt" #Totaly work!
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'