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
  How do I handle escape character in parameter arguments in Python? JKR 6 1,140 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  How to display <IPython.core.display.HTML object>? pythopen 3 45,931 May-06-2023, 08:14 AM
Last Post: pramod08728
  use of escape character in re.sub and find WJSwan 1 905 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  detect equal sequences in list flash77 17 2,794 Oct-28-2022, 06:38 AM
Last Post: flash77
  How can I display dictionary in a gui window in Python? C0D3R 2 1,723 Apr-07-2022, 07:33 PM
Last Post: C0D3R
  Escape indentation Frankduc 11 3,070 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  add Escape charcters in string GrahamL 3 1,164 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Escape Single quotation between each content tag usman 3 2,793 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 2,299 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 2,020 Dec-23-2020, 06:47 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

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