Why can't Python's raw string literals end with a single backslash?

后端 未结 12 1382
后悔当初
后悔当初 2020-11-22 07:37

Technically, any odd number of backslashes, as described in the documentation.

>>> r\'\\\'
  File \"\", line 1
    r\'\\\'
       ^
Syn         


        
12条回答
  •  故里飘歌
    2020-11-22 08:05

    Another trick is to use chr(92) as it evaluates to "\".

    I recently had to clean a string of backslashes and the following did the trick:

    CleanString = DirtyString.replace(chr(92),'')
    

    I realize that this does not take care of the "why" but the thread attracts many people looking for a solution to an immediate problem.

提交回复
热议问题