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
  help for escape sequences NewPi 1 240 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  copying parts of mutable sequences Skaperen 1 293 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  Convert weekly sequences to date and time. SinPy 0 172 Nov-23-2019, 05:20 PM
Last Post: SinPy
  escape single quote deep_logic 1 254 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU
  Beginner Question - Esaping the Escape Character correctly? Bramen 4 453 Aug-27-2019, 02:38 PM
Last Post: Bramen
  The use of escape char \ hishamzero1 2 524 Aug-12-2019, 10:20 PM
Last Post: hishamzero1
  type for sequences Skaperen 3 647 Oct-19-2018, 03:36 AM
Last Post: Skaperen
  How to display <IPython.core.display.HTML object>? pythopen 2 20,624 Oct-17-2018, 06:22 PM
Last Post: pythopen
  Python 3 escape codes oldDog 1 700 Sep-15-2018, 10:12 AM
Last Post: gruntfutuk
  OSX: copied path has escape character ronjan 4 1,164 Mar-14-2018, 06:23 PM
Last Post: ronjan

Forum Jump:


Users browsing this thread: 1 Guest(s)