Why do numbers in a string become “x0n” when a backslash precedes them?

前端 未结 2 961
暗喜
暗喜 2020-12-06 22:03

I was doing a few experiments with escape backslashes in the Python 3.4 shell and noticed something quite strange.

>>> string = \"\\test\\test\\1\\2         


        
2条回答
  •  独厮守ぢ
    2020-12-06 23:07

    When you write

    string = "\test\test\1\2\3"
    

    Python thinks that you want to define a string of characters that starts with the tab character ("\t") then the character "e", then "s", and so on. Python also thinks that you want to include some non-printable characters corresponding to the literal numbers 1, 2, and 3, which the shorthand "\1", "\2" and "\3" provides.

提交回复
热议问题