Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Escape sequences display in python
#1
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'
Quote
#2
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]);
            }

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  escape single quote deep_logic 1 118 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 277 Aug-27-2019, 02:38 PM
Last Post: Bramen
  The use of escape char \ hishamzero1 2 345 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  How To Display this Python Code in Web browser? pradhan 1 269 Jul-14-2019, 06:59 PM
Last Post: snippsat
  type for sequences Skaperen 3 455 Oct-19-2018, 03:36 AM
Last Post: Skaperen
  How to display <IPython.core.display.HTML object>? pythopen 2 11,675 Oct-17-2018, 06:22 PM
Last Post: pythopen
  Python 3 escape codes oldDog 1 555 Sep-15-2018, 10:12 AM
Last Post: gruntfutuk
  OSX: copied path has escape character ronjan 4 928 Mar-14-2018, 06:23 PM
Last Post: ronjan
  Yaml File escape characters in multiline string harun2525 0 4,152 May-09-2017, 10:59 PM
Last Post: harun2525
  comparing strings (or sequences) Skaperen 5 1,754 Jan-25-2017, 10:30 AM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)