>>> a = rb"\x3cdiv\x3e"
>>> a.decode('unicode_escape')
'<div>'

Also check out some interesting codecs.

Answer from Kabie on Stack Overflow
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › ascii-encoding--python
ASCII Encoding : Python | Encoding Solutions Across Programming Languages
Here’s how to decode ASCII bytes: # ASCII encoded byte string ascii_encoded = b'Hello, World!' # Decode the byte string back to a regular string decoded_text = ascii_encoded.decode('ascii') # Display the decoded string print(decoded_text) # Output: Hello, World!
🌐
AskPython
askpython.com › python › examples › converting-bytes-ascii-unicode
Converting Bytes to Ascii or Unicode - AskPython
March 25, 2023 - Now that we have knowledge about byte objects, ASCII and Unicode, let us learn how to convert byte objects into ASCII and Unicode. The decode() method can be used to convert the byte object into ASCII.
🌐
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'
🌐
SSOJet
ssojet.com › character-encoding-decoding › ascii-in-python
ASCII in Python | Encoding Standards for Programming Languages
The most common encoding scheme used today is UTF-8, which can represent virtually any character from any language. Python's built-in string objects have an .encode() method to handle this conversion. To reverse the process, turning bytes back into a readable string, you use the .decode() method.
🌐
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.
Find elsewhere
🌐
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')
🌐
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)
🌐
Medium
medium.com › @jitendrarajput588 › 3-ways-to-convert-ascii-codes-to-text-in-python-912dd54e1ac2
3 ways to convert ASCII codes to text in Python | by Jitendra Rajput | Medium
September 18, 2023 - 3 ways to convert ASCII codes to text in Python In this article, we will discuss 3 ways of ASCII-to-text conversion using Python. When you want to convert ASCII values into text online. Use a …
🌐
Quora
quora.com › How-do-I-convert-ASCII-code-in-Python
How to convert ASCII code in Python - Quora
Answer (1 of 6): You can use chr() or ord() methods to convert ASCII code conversions. Let’s start, 1. chr(): The chr method returns a string representing a character whose Unicode point is an integer. It takes only one integer as a parameter. 2. ord(): The ord method accepts a string of units ...
🌐
Python Pool
pythonpool.com › home › tutorials › ascii to string in python: chr(), ord(), bytes, and validation
ASCII to String in Python: chr(), ord(), Bytes, and Validation
July 13, 2026 - Quick answer: Use chr(code) to convert an integer Unicode code point into a Python string and ord(character) to convert one character back to its code point. ASCII is the 0 through 127 subset, so validate that range when the input must be ASCII-only.
🌐
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.
🌐
Python
docs.python.org › 3 › library › binascii.html
binascii — Convert between binary and ASCII
Convert binary data to a line of ASCII characters in base64 coding. The return value is the converted line, including a newline char if newline is true. The output of this function conforms to RFC 3548.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-convert-list-of-ascii-value-to-string
Ways to Convert List of ASCII Value to String - Python - GeeksforGeeks
July 12, 2025 - Explanation: bytearray(a) creates a bytearray from the list a and .decode() converts the bytearray into a string by interpreting the bytes as characters. A traditional approach to convert ASCII values to a string is to use a for-loop to iterate ...
🌐
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.
Top answer
1 of 1
5
  1. return '' doesn't have the type List.
  2. List means List[Any], you should use List[str].
  3. Your variable names are garbage.

    • s -> string
    • n -> length
    • ch_map -> char_map or character_map
    • dp -> what does this even mean?
    • p1 -> position_1?
  4. Don't put return statements on the same line as an if.

  5. Again strings shouldn't be assigend to lists, yes you can iterate over them because they're both sequences. But they're not the same type. [ch_map[s[:2]]] if s[:2] in char_map else ''
  6. ch_map should be a constant outside the function.
  7. It's far easier to understand your code if it's written using recursion.
  8. Recursion has some problems, and so it should be written in a way that allows you to easily convert it to a while loop.
CHAR_MAP = {str(i): chr(i) for i in range(10, 127)}


def asciidecode(string: str) -> List[str]:
    if not string:
        return []
    string = string[::-1]
    length = len(string)

    def inner(index):
        if index == length:
            yield ''
        else:
            for size in (2, 3):
                if length < index + size:
                    break
                letter = CHAR_MAP.get(string[index:index + size])
                if letter is not None:
                    for word in inner(index + size):
                        yield letter + word
    return list(inner(0))


def asciidecode_pre_while(string: str) -> List[str]:
    if not string:
        return []
    string = string[::-1]
    length = len(string)
    output = []

    def inner(index, word):
        if index == length:
            output.append(word)
            return

        for size in (2, 3):
            if length < index + size:
                break
            letter = CHAR_MAP.get(string[index:index + size])
            if letter is not None:
                inner(index + size, word + letter)
    inner(0, '')
    return output

From the second one it's easy to convert it to a while loop:

CHAR_MAP = {str(i): chr(i) for i in range(10, 127)}


def asciidecode(string: str) -> List[str]:
    if not string:
        return []
    string = string[::-1]
    length = len(string)
    output = []

    stack = [(0, '')]
    while stack:
        index, word = stack.pop()
        if index == length:
            output.append(word)
            continue

        for size in (2, 3):
            if length < index + size:
                break
            letter = CHAR_MAP.get(string[index:index + size])
            if letter is not None:
                stack.append((index + size, word + letter))
    return output