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


Messages In This Thread
Escape sequences display in python - by Uchikago - Jun-27-2019, 02:57 PM
RE: Escape sequences display in python - by Gribouillis - Jun-27-2019, 03:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I handle escape character in parameter arguments in Python? JKR 6 1,118 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
  How to display <IPython.core.display.HTML object>? pythopen 3 45,886 May-06-2023, 08:14 AM
Last Post: pramod08728
  use of escape character in re.sub and find WJSwan 1 904 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  detect equal sequences in list flash77 17 2,784 Oct-28-2022, 06:38 AM
Last Post: flash77
  How can I display dictionary in a gui window in Python? C0D3R 2 1,712 Apr-07-2022, 07:33 PM
Last Post: C0D3R
  Escape indentation Frankduc 11 3,052 Jan-31-2022, 02:41 PM
Last Post: Frankduc
  add Escape charcters in string GrahamL 3 1,159 Jan-20-2022, 01:15 PM
Last Post: GrahamL
  Escape Single quotation between each content tag usman 3 2,781 May-02-2021, 03:32 PM
Last Post: snippsat
  DIY Escape Room for fun StannemanPython 1 2,293 Feb-17-2021, 10:53 PM
Last Post: maurom82
  How to escape OrderedDict as an argument? Mark17 2 2,014 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