Python Forum
Escape sequences display in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Escape sequences display in python (/thread-19414.html)



Escape sequences display in python - Uchikago - Jun-27-2019

Can you guys explain why both \n and \b are escape sequences but why \n displays as '\n' but \b displays as '\x08'
>>> '\n'
'\n'
>>> '\b'
'\x08'



RE: Escape sequences display in python - Gribouillis - Jun-27-2019

In C-Python, I think it's because of this section of code in function unicode_repr() of unicodeobject.c. The method str.__repr__() gives one of the possible literal
representation of the string. As there are several possible literal representation of the string, an arbitrary choice has been made. The authors of this code considered probably that \t and \n were far more frequent than \b in user code, otherwise we would also have \x09 and \x0a for these characters.
else {
        for (i = 0, o = 1; i < isize; i++) {
            Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);

            /* Escape quotes and backslashes */
            if ((ch == quote) || (ch == '\\')) {
                PyUnicode_WRITE(okind, odata, o++, '\\');
                PyUnicode_WRITE(okind, odata, o++, ch);
                continue;
            }

            /* Map special whitespace to '\t', \n', '\r' */
            if (ch == '\t') {
                PyUnicode_WRITE(okind, odata, o++, '\\');
                PyUnicode_WRITE(okind, odata, o++, 't');
            }
            else if (ch == '\n') {
                PyUnicode_WRITE(okind, odata, o++, '\\');
                PyUnicode_WRITE(okind, odata, o++, 'n');
            }
            else if (ch == '\r') {
                PyUnicode_WRITE(okind, odata, o++, '\\');
                PyUnicode_WRITE(okind, odata, o++, 'r');
            }

            /* Map non-printable US ASCII to '\xhh' */
            else if (ch < ' ' || ch == 0x7F) {
                PyUnicode_WRITE(okind, odata, o++, '\\');
                PyUnicode_WRITE(okind, odata, o++, 'x');
                PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[(ch >> 4) & 0x000F]);
                PyUnicode_WRITE(okind, odata, o++, Py_hexdigits[ch & 0x000F]);
            }