the reason: python string assignments accidentally change '\b' into '\x08' and '\a' into '\x07', why Python did this?

前端 未结 2 512
不知归路
不知归路 2020-12-11 01:28

Had two answers and some comments, mentioned another question, but all had not provided REASON, why Python did this changes? such as \'/b\' is \'/x08\' is just the result, b

2条回答
  •  伪装坚强ぢ
    2020-12-11 01:59

    Escape sequence \a, \b is equivalnt to \x07, \x08.

    >>> '\a'
    '\x07'
    >>> '\b'
    '\x08'
    

    You should escape \ itself to represent backslash literally:

    >>> '\\a'
    '\\a'
    >>> '\\b'
    '\\b'
    

    or use raw string literals:

    >>> r'\a'
    '\\a'
    >>> r'\b'
    '\\b'
    

提交回复
热议问题