Python Forum
Escape sequences display in python
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'
Reply
#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]);
            }
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Escape Single quotation between each content tag usman 3 434 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 495 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 477 Dec-23-2020, 06:47 PM
Last Post: Mark17
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 489 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  needleman wunsch algorithm for two sequences of different length johnny_sav1992 0 552 Jul-27-2020, 05:45 PM
Last Post: johnny_sav1992
  python display with '\\' when prints with key-value in dictionary maiya 12 2,582 May-30-2020, 05:56 PM
Last Post: maiya
  help for escape sequences NewPi 1 755 Dec-11-2019, 11:22 PM
Last Post: ichabod801
  copying parts of mutable sequences Skaperen 1 878 Dec-02-2019, 10:34 AM
Last Post: Gribouillis
  Convert weekly sequences to date and time. SinPy 0 559 Nov-23-2019, 05:20 PM
Last Post: SinPy
  escape single quote deep_logic 1 723 Sep-10-2019, 08:05 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020