According to my interpretation of the implementation of unicode-escape and the unicode repr in the CPython 2.6.5 source, yes; the only difference between repr(unicode_string) and unicode_string.encode('unicode-escape') is the inclusion of wrapping quotes and escaping whichever quote was used.

They are both driven by the same function, unicodeescape_string. This function takes a parameter whose sole function is to toggle the addition of the wrapping quotes and escaping of that quote.

Answer from Mike Boers on Stack Overflow
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί howto β€Ί unicode.html
Unicode HOWTO β€” Python 3.14.7 documentation
In Python source code, specific Unicode code points can be written using the \u escape sequence, which is followed by four hex digits giving the code point.
🌐
Teleport
goteleport.com β€Ί home β€Ί resources β€Ί tools β€Ί unicode escape/unescape | encode/decode special characters
Unicode Escape/Unescape | Encode/Decode Special Characters | Teleport
Using escape sequences for special characters in data formats like JSON guarantees that the information is correctly understood by all systems involved. When you incorporate a Unicode escape sequence into your code, the programming language's interpreter or compiler recognizes it during the parsing or compilation process.
Discussions

How to cast escaped unicode characters embeded into an string?
Use str.encode(). >>> "Some text\r\n word word word\u0022".encode("utf-8") b'Some text\r\n word word word"' More on reddit.com
🌐 r/learnpython
3
2
March 5, 2022
encoding - Python "string_escape" vs "unicode_escape" - Stack Overflow
According to the docs, the builtin string encoding string_escape: Produce[s] a string that is suitable as string literal in Python source code ...while the unicode_escape: Produce[s] a string th... More on stackoverflow.com
🌐 stackoverflow.com
How do convert unicode escape sequences to unicode characters in a python string - Stack Overflow
When I tried to get the content of a tag using "unicode(head.contents[3])" i get the output similar to this: "Christensen Sk\xf6ld". I want the escape sequence to be returned as string. How to do it in python? More on stackoverflow.com
🌐 stackoverflow.com
Escaping unicode strings in python - Stack Overflow
In python these three commands print the same emoji: print "\xF0\x9F\x8C\x80" πŸŒ€ print u"\U0001F300" πŸŒ€ print u"\ud83c\udf00" πŸŒ€ How can I translate between \x, \u and \U escaping? I can't figure ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Michael Currin
michaelcurrin.github.io β€Ί dev-cheatsheets β€Ί cheatsheets β€Ί python β€Ί strings β€Ί encoding β€Ί unicode.html
Unicode | Dev Cheatsheets
A unicode character will be displayed in human-readable form. ... If you make it a raw string, then Python will escape it.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί how to cast escaped unicode characters embeded into an string?
r/learnpython on Reddit: How to cast escaped unicode characters embeded into an string?
March 5, 2022 -

Sorry for the weird question.

I got a string like this one:

Some text\r\n word word word\u0022

I think those backslashed characters are escape sequences, and i need to convert/cast them to the character they represent.

For example, i think the \u0022 is the doble quotes ( " ), so i need to convert the string to this:

Some text\r\n word word word"

Is it possible to do this whithout having to replace every character manually (with replace() string method)?

I don't know if i could convert the breakline. In that particular case, there wouldn't be a problem if i just replace it with a space, but i need to cast every other character.

I hope you could understand what i mean. Thanks in advance and sorry for this weird and tricky question.

🌐
Python Basics
python-basics-tutorial.readthedocs.io β€Ί en β€Ί latest β€Ί types β€Ί strings β€Ί encodings.html
Unicode and character encodings - Python Basics
Special characters and escape sequences:\n stands for the newline character and\t for the tab character. Character sequences that begin with a backslash and are used to represent other characters a...
🌐
MojoAuth
mojoauth.com β€Ί escaping β€Ί unicode-escaping-in-python
Unicode Escaping in Python | Escaping Methods in Programming Languages
In Python, Unicode escaping is achieved using a backslash (``) followed by a specific code point. The syntax typically looks like uXXXX for characters in the Basic Multilingual Plane (BMP), where XXXX is a four-digit hexadecimal number.
Find elsewhere
Top answer
1 of 4
12

The first one is a byte string:

>>> "\xF0\x9F\x8C\x80".decode('utf8')
u'\U0001f300'

The u"\ud83c\udf00" one is the UTF16 version (four digit unicode escape)

The u"\U0001F300" one is actual index of the codepoint.


But how do the numbers relate? This is the difficult question. It's defined by the encoding and there is no obvious relationship. To give you an idea, here is an example of "manually" encoding the codepoint at index 0x1F300 into UTF-8:

The cyclone character πŸŒ€ has index 0x1f300 which falls into the range 0x00010000 - 0x001FFFFF. The template for this range is:

11110... 10...... 10...... 10......

Where you fill in the dots with the binary representation of the codepoint. I can't tell you why the template looks like that, it's just the utf-8 definition.

Here's the binary representation of our codepoint:

>>> u'πŸŒ€'
u'\U0001f300'
>>> unichr(0x1f300)
u'\U0001f300'
>>> bin(0x1f300)
'0b11111001100000000'

So if we take the string template and fill it up like this (with some leading zeros because there are more slots in the template than significant digits in our number) we get this:

11110... 10...... 10...... 10......
11110000 10011111 10001100 10000000

Now let's convert that back to hex

>>> 0b11110000100111111000110010000000
4036988032
>>> hex(4036988032)
'0xf09f8c80'

And there you have the UTF8 representation of the codepoint.

For UTF16 there is a similar magic recipe for your codepoint: 0x10000 is subtracted from the index, and then we pad with zeros to get a 20-bit binary representation. The first ten bits are added to 0xD800 to give the first 16-bit code unit. The last ten bits are added to 0xDC00 to give the second 16-bit code unit.

>>> bin(0x1f300 - 0x10000)[2:].rjust(20, '0')
'00001111001100000000'
>>> _[:10], _[10:]
('0000111100', '1100000000')
>>> hex(0b0000111100 + 0xd800)
'0xd83c'
>>> hex(0b1100000000 + 0xdc00)
'0xdf00'

And there's your UTF 16 version, i.e. the one with the lowercase \u escape.

As you can probably understand there may be no obvious numerical relationship between the hex digits in these representations, they are just different encodings of the same code point.

2 of 4
3

See Unicode Literals in Python Source Code

In Python source code, Unicode literals are written as strings prefixed with the β€˜u’ or β€˜U’ character: u'abcdefghijk'. Specific code points can be written using the \u escape sequence, which is followed by four hex digits giving the code point. The \U escape sequence is similar, but expects 8 hex digits, not 4.

In [1]: "\xF0\x9F\x8C\x80".decode('utf-8')
Out[1]: u'\U0001f300'

In [2]: u'\U0001F300'.encode('utf-8')
Out[2]: '\xf0\x9f\x8c\x80'

In [3]: u'\ud83c\udf00'.encode('utf-8')
Out[3]: '\xf0\x9f\x8c\x80'

\uhhhh     --> Unicode character with 16-bit hex value  
\Uhhhhhhhh --> Unicode character with 32-bit hex value

In Unicode escapes, the first form gives four hex digits to encode a 2-byte (16-bit) character code point, and the second gives eight hex digits for a 4-byte (32-bit) code point. Byte strings support only hex escapes for encoded text and other forms of byte-based data

🌐
SSOJet
ssojet.com β€Ί escaping β€Ί unicode-escaping-in-python
Unicode Escaping in Python | Escaping Techniques in Programming
If utf8_representation was encoded with UTF-8, attempting to decode it with 'latin-1' will likely result in a UnicodeDecodeError or garbled characters. Always ensure your encoding and decoding methods match. Python offers straightforward ways to embed non-ASCII characters directly into your source code. For characters within the Basic Multilingual Plane (BMP), you'll use the \uXXXX escape sequence, where XXXX represents the four hexadecimal digits of the character's code point.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί replacing literal '\u****' in string with corresponding unicode character
r/learnpython on Reddit: Replacing literal '\u****' in string with corresponding Unicode character
March 13, 2020 -

Instead of scraping entirely via xpath like I have in the past, I decided to pull the data out of a clearly visible var in a <script> element and write a function to parse it as a dictionary recursively*.

My problem is that many characters are escaped, so instead of https:// I have https:\u002F\u002F, literally -- 12 characters instead of those 2 forward slashes, the \u**** sequence doesn't stand for the frontslashes. I have this problem in body text too, and because there are lots of diacritical marks I can't get away with replacing them bluntly. I need to replace these substrings with the corresponding characters, so \u002F -> /, \u00C9 -> Γ©, etc.

It seems like there should be an obvious and not-ridiculous solution for this, but I don't know what it is and googling turns up different problems. Is this something I need to anticipate at the scraping stage? I'm using the requests and html modules if it matters.

*Which is dumb because it was already formatted just like a dict, so I could probably just have saved the raw text in a .py and imported it as a module

🌐
Super User
superuser.com β€Ί questions β€Ί 1759598 β€Ί unicode-escaping-with-a-standard-unix-tool
python - unicode-escaping with a standard Unix tool - Super User
December 26, 2022 - #!/usr/bin/env python3 import sys text = sys.stdin.read() new_text = text.encode('unicode-escape').decode() sys.stdout.write(new_text)
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί how-to-fix-syntaxerror-unicode-error-unicodeescape-codec-cant-decode-bytes
How to Fix SyntaxError: (Unicode Error) 'Unicodeescape' Codec Can't Decode Bytes - GeeksforGeeks
July 1, 2026 - Unicodeescape error usually occurs when Python encounters an invalid escape sequence inside a string. This is most commonly seen when working with Windows file paths because backslashes (\) are interpreted as escape characters.
🌐
Real Python
realpython.com β€Ί python-encodings-guide
Unicode & Character Encodings in Python: A Painless Guide – Real Python
May 20, 2019 - This also means that the "\Uxxxxxxxx" form is the only escape sequence that is capable of holding any Unicode character. Note: Here’s a short function to convert strings that look like "U+10346" into something Python can work with.
🌐
EDUCBA
educba.com β€Ί home β€Ί software development β€Ί software development tutorials β€Ί python tutorial β€Ί python unicode error
Python Unicode Error | Working of Unicode Error in Python with Examples
January 8, 2024 - To include Unicode characters in the Python program, we first use the Unicode escape symbol \ you before any string, which can be considered a Unicode-type variable.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
LearnByExample
learnbyexample.github.io β€Ί py_regular_expressions β€Ί unicode.html
Unicode - Understanding Python re(gex)?
You can use escapes \u and \U to specify Unicode characters with 4 and 8 hexadecimal digits respectively.
🌐
Python
docs.python.org β€Ί 3 β€Ί c-api β€Ί unicode.html
Unicode Objects and Codecs β€” Python 3.14.7 documentation
Encode a Unicode object using Raw-Unicode-Escape and return the result as a bytes object.