pythonprefix

Deal with last character of backslash in "r" prefix and "+" in Python


From the link, the r prefix is about raw string. Actually, there are some situations that cause me very difficult to understand the r's function. Let me express my opinion:

  1. 'H:\\Education' equals r'H:\Education', as r prefix means not convert characters. But, if tested in Python, 'H:\Education' equals 'H:\\Education'. What is the function of r here, \ equal \\ no matter there is r leading?
  2. As 'H:\Education' equals 'H:\\Education', 'H:\Education\' should equal 'H:\\Education\\' or 'H:\Education\\' or 'H:\\Education\', but actually, these four are not the same in Python; Why? Is it about the location of \ or \\?
  3. If r does nothing in list 1, why r'C:\Program Files\7-Zip\7z.exe' is right but 'C:\Program Files\7-Zip\7z.exe' is not right?
  4. r'H:\Education\' is wrong, why?

So, sometimes r has function, sometimes not. How can I tell them and make the right choice?


Solution

  • The details are in the docs at String and Bytes literals. Pay particular attention to the recognized escape sequences table a few pages down. Here are some rules

    1. \\ is converted to \
    2. \ooo (with o being octal digits) is converted to octal
    3. unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the result.
    4. Even in an r literal, quotes can be escaped with a backslash, but the backslash remains in the result

    In...

    It's something of a dark art. The reason why people double-escape all backslashes in Windows paths is that it is easy to miss which things don't change because of rule 3 verses the multiple things that do change. Your 7z issue is a classic case. Better to double escape everything that run the risk you missed one of the rules.