>>> a = rb"\x3cdiv\x3e"
>>> a.decode('unicode_escape')
'<div>'
Also check out some interesting codecs.
Answer from Kabie on Stack Overflow Top answer 1 of 2
18
>>> a = rb"\x3cdiv\x3e"
>>> a.decode('unicode_escape')
'<div>'
Also check out some interesting codecs.
2 of 2
7
With python 3.x, you would adapt Kabie answer to
a = b"\x3cdiv\x3e"
a.decode('unicode_escape')
or
a = b"\x3cdiv\x3e"
a.decode('ascii')
both give
>>> a
b'<div>'
What is b prefix for ?
Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.
01:38
Converting ASCII to String in Python - YouTube
03:14
How Can I Convert Characters To ASCII Values In Python? - Next ...
01:50
ENCODE And DECODE Your Secrets In Python With ASCII Cipher - YouTube
01:22
ENCODE & DECODE Like a Pro with Python’s Base64 Trick - YouTube
03:10
python decode extended ascii - YouTube
GeeksforGeeks
geeksforgeeks.org › python › python-strings-decode-method
Python Strings decode() method - GeeksforGeeks
May 11, 2026 - It is later decoded to verify the login. Note: In real applications, passwords should be stored using hashing (e.g., Django’s built-in password hashing or algorithms like bcrypt), not encoding · Helps retrieve the original text from an encoded format. Essential for handling different character encodings like UTF-8, ASCII...
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › decode.html
decode — Python Reference (The Right Way) 0.1 documentation
>>> 'źdźbło'.decode('windows-1250') # polish word meaning a blade of grass u'\u0139\u015fd\u0139\u015fb\u0139\u201ao' >>> 'źdźbło'.decode('ascii', 'strict') Traceback (most recent call last): File "<interactive input>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128) >>> 'źdźbło'.decode('ascii', 'ignore') u'dbo' >>> 'źdźbło'.decode('ascii', 'replace') u'\ufffd\ufffdd\ufffd\ufffdb\ufffd\ufffdo'
Medium
medium.com › @dave1off › implementing-character-encodings-with-python-part-1-ascii-utf-8-e8deaa97ccdc
Implementing character encodings with Python | Part 1 — ASCII & UTF-8 | by Dave Chupreev | Medium
August 8, 2020 - We know that it will take one byte to encode code points in the interval from 0x00 to 0x7f with 0 leading bit, i.e. 7 bits left. And we know that ASCII characters take 7 bits. Guess what? Yes, ASCII is fully compatible with UTF-8. So if you encode file with ASCII, you can decode it with UTF-8 for sure.
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.7 documentation
If you can’t enter a particular ... code ASCII-only for some reason, you can also use escape sequences in string literals. (Depending on your system, you may see the actual capital-delta glyph instead of a u escape.) >>> "\N{GREEK CAPITAL LETTER DELTA}" # Using the character name '\u0394' >>> "\u0394" # Using a 16-bit hex value '\u0394' >>> "\U00000394" # Using a 32-bit hex value '\u0394' In addition, one can create a string using the decode() method ...
Reddit
reddit.com › r/learnpython › decode and encode with python
r/learnpython on Reddit: Decode and Encode with Python
July 28, 2022 -
I am getting a byte base64 encoded data and I have to decode it but it is showing an error on the last line.
Error - UnicodeDecodeError: 'ascii' codec can't decode byte 0x82 in position 1: ordinal not in range(128)
base64_bytes = base64_message.encode('ascii')
print(base64_bytes)
#decode
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('ascii') Top answer 1 of 3
4
You don't need to encode a base64 message, it's already ascii by definition. But the result you get from decoding matches the original message, and so isn't necessarily ascii. Assuming you originally encoded it into utf-8, you'd need to decode it back from utf-8 not ascii.
2 of 3
2
There is a package called Chardet which might be useful: https://pypi.org/project/chardet/
Python Central
pythoncentral.io › encoding-and-decoding-strings-in-python-3-x
Encoding and Decoding Strings (in Python 3.x) | Python Central
December 29, 2021 - As a final note on strings in Python 3.x and Python 2.x, we must be sure to remember that using the open method for writing to files in both branches will not allow for Unicode strings (that contain non-ASCII characters) to be written to files. In order to do this the strings must be encoded. This is no big deal in Python 2.x, as a string will only be Unicode if you make it so (by using the unicode method or str.decode), but in Python 3.x all strings are Unicode by default, so if we want to write such a string, e.g.
AskPython
askpython.com › python › string › python-encode-and-decode-functions
Python encode() and decode() Functions - AskPython
February 16, 2023 - a = 'This is a bit möre cömplex sentence.' print('Original string:', a) # Encoding in UTF-8 encoded_bytes = a.encode('utf-8', 'replace') # Trying to decode via ASCII, which is incorrect decoded_incorrect = encoded_bytes.decode('ascii', 'replace') decoded_correct = encoded_bytes.decode('utf-8', 'replace') print('Incorrectly Decoded string:', decoded_incorrect) print('Correctly Decoded string:', decoded_correct)
Python Module of the Week
pymotw.com › 2 › codecs
codecs – String encoding and decoding - Python Module of the Week
Now available for Python 3! Buy the book! ... The codecs module provides stream and file interfaces for transcoding data in your program. It is most commonly used to work with Unicode text, but other encodings are also available for other purposes. CPython 2.x supports two types of strings for working with text data. Old-style str instances use a single 8-bit byte to represent each character of the string using its ASCII code.
Real Python
realpython.com › python-encodings-guide
Unicode & Character Encodings in Python: A Painless Guide – Real Python
May 20, 2019 - *Such as English, Arabic, Greek, and Irish **A huge array of languages and symbols—mostly Chinese, Japanese, and Korean by volume (also ASCII and Latin alphabets) ***Additional Chinese, Japanese, Korean, and Vietnamese characters, plus more symbols and emojis · Note: In the interest of not losing sight of the big picture, there is an additional set of technical features of UTF-8 that aren’t covered here because they are rarely visible to a Python user. For instance, UTF-8 actually uses prefix codes that indicate the number of bytes in a sequence. This enables a decoder to tell what bytes belong together in a variable-length encoding, and lets the first byte serve as an indicator of the number of bytes in the coming sequence.