Python “string_escape” vs “unicode_escape”

前端 未结 2 512
星月不相逢
星月不相逢 2020-12-12 20:28

According to the docs, the builtin string encoding string_escape:

Produce[s] a string that is suitable as string literal in Python source

2条回答
  •  北海茫月
    2020-12-12 21:04

    According to my interpretation of the implementation of unicode-escape and the unicode repr in the CPython 2.6.5 source, yes; the only difference between repr(unicode_string) and unicode_string.encode('unicode-escape') is the inclusion of wrapping quotes and escaping whichever quote was used.

    They are both driven by the same function, unicodeescape_string. This function takes a parameter whose sole function is to toggle the addition of the wrapping quotes and escaping of that quote.

提交回复
热议问题