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

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

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

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


        
12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 08:20

    Comming from C it pretty clear to me that a single \ works as escape character allowing you to put special characters such as newlines, tabs and quotes into strings.

    That does indeed disallow \ as last character since it will escape the " and make the parser choke. But as pointed out earlier \ is legal.

提交回复
热议问题