It's an encoding error - so if it's a unicode string, this ought to fix it:

text.encode("windows-1252").decode("utf-8")

If it's a plain string, you'll need an extra step:

text.decode("utf-8").encode("windows-1252").decode("utf-8")

Both of these will give you a unicode string.

By the way - to discover how a piece of text like this has been mangled due to encoding issues, you can use chardet:

>>> import chardet
>>> chardet.detect(u"And the Hip’s coming, too")
{'confidence': 0.5, 'encoding': 'windows-1252'}
Answer from Zero Piraeus on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-strings-decode-method
Python Strings decode() method - GeeksforGeeks
May 11, 2026 - Encoding converts a string into bytes and decode() brings it back to its original form. ... t = "Hello, Python!" e_t = t.encode('utf-8') print("Encoded:", e_t) d_t = e_t.decode('utf-8') print("Decoded:", d_t)
Discussions

UTF-8 decoding
The error is telling you the bytes you read with the f.read(name_len) was not a utf-8 encoded string. Probably you should check what the contents of the file f is, and possibly print out name_len and f.read(name_len) to check what you read is what you thought it should be. More on reddit.com
🌐 r/learnpython
7
3
June 24, 2024
how to decode UTF-8?
I don’t have an interpreter with me right now on mobile but I could’ve sworn you could do: X=“test” X.encode(“utf-8”) X.decode(“utf-8”) More on reddit.com
🌐 r/learnpython
7
6
June 20, 2018
The use of open(encoding="utf-8")
open defaults to whatever encoding your system uses by default, so it can be anything from ASCII to ISO-8859-1. You can, however, make it use a specific encoding instead. utf-8 is useful as it has most characters. More on reddit.com
🌐 r/learnpython
16
3
November 2, 2020
🌐
Python documentation
docs.python.org › 3 › howto › unicode.html
Unicode HOWTO — Python 3.14.6 documentation
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte >>> b'\x80abc'.decode("utf-8", "replace") '\ufffdabc' >>> b'\x80abc'.decode("utf-8", "backslashreplace") '\\x80abc' >>> b'\x80abc'.decode("utf-8", "ignore") 'abc'
🌐
Mimo
mimo.org › glossary › python › string-decode
Python string decode(): Syntax, Usage, and Examples
Start your coding journey with Python. Learn basics, data types, control flow, and more ... message = b'Hello, world!' decoded_message = message.decode('utf-8') print(decoded_message) # Output: Hello, world!
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-string-encode-decode
Python String Encode and Decode: Complete Guide | DigitalOcean
August 3, 2022 - Some other possible values are ‘ignore’, ‘replace’ and ‘xmlcharrefreplace’. Let’s look at a simple example of python string encode() decode() functions. str_original = 'Hello' bytes_encoded = str_original.encode(encoding='utf-8') print(type(bytes_encoded)) str_decoded = bytes_encoded.decode() print(type(str_decoded)) print('Encoded bytes =', bytes_encoded) print('Decoded String =', str_decoded) print('str_original equals str_decoded =', str_original == str_decoded)
🌐
Python
docs.python.org › 3 › library › codecs.html
codecs — Codec registry and base classes
Creates a StreamRecoder instance which implements a two-way conversion: encode and decode work on the frontend — the data visible to code calling read() and write(), while Reader and Writer work on the backend — the data in stream. You can use these objects to do transparent transcodings, e.g., from Latin-1 to UTF-8 and back.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › utf-8 decoding
r/learnpython on Reddit: UTF-8 decoding
June 24, 2024 -

Hi! So I'm trying to decode some utf-8 strings in Python. However some bring up an error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 3: invalid start byte or something along the lines of that. Here's the function: def b_numstring(f):

name_len = int.from_bytes(f.read(4), byteorder='big')

string = f.read(name_len).decode('utf-8')

return string Any help would be appreciated. Thanks!

🌐
Reddit
reddit.com › r/learnpython › how to decode utf-8?
r/learnpython on Reddit: how to decode UTF-8?
June 20, 2018 -

Hello,

I've been struggling to figure this one out. This is pulling from the google API and extracting from my gmail account the message. As per the documentation, I have been able to locate the body of the message as per some of their references here.

Below is the snippet of how it looks. I've shortened it to be more user friendly.

'headers': [{'name': 'Content-Type', 'value': 'text/plain; charset="utf-8"'},

{'name': 'Content-Transfer-Encoding', 'value': '8bit'}],

'body': {'size': 3381,

'data': 'Shortened gobbledigookDQcz1lNj=='}}]}, 'sizeEstimate': 22874}

import base64

body=email_message['payload']['parts'][0]['body']['data']
translated_body = base64.b64decode(bytes(body), 'UTF-8')

From what I can see, my body variable shoots out the long alphanumeric string that should be the email body. However, I keep getting this error:

TypeError: string argument without an encoding

Any idea why? If it helps to include more code I can, but I think this is the only relevant part that is giving me issues.

Figured it out through more searching on the web!

msg_str = str(base64.urlsafe_b64decode(message['raw'].encode('ASCII')),'UTF-8')
mime_msg = email.message_from_string(msg_str)
🌐
Honeybadger
honeybadger.io › blog › python-character-encoding
Python developer's guide to character encoding - Honeybadger Developer Blog
March 6, 2023 - To decode bytes into strings, call the decode() method and specify the type of character encoding you wish to use. Of course, we are using UTF-8.
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › utf-8-encoding--python
UTF-8 Encoding : Python | Encoding Solutions Across Programming Languages
\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x8c\xe4\xb8\x96\xe7\x95\x8c\xef\xbc\x81' # Decoding the bytes back to a string decoded_text = encoded_text.decode('utf-8') # Displaying the decoded string print(decoded_text) # Output: Hello, World! 你好,世界! · In this example, the previously encoded bytes are decoded back to their original string format, demonstrating the seamless transition between encoding and decoding in Python.
🌐
GitHub
github.com › openai › openai-python
GitHub - openai/openai-python: The official Python library for the OpenAI API · GitHub
May 28, 2026 - import base64 from openai import OpenAI client = OpenAI() prompt = "What is in this image?" with open("path/to/image.png", "rb") as image_file: b64_image = base64.b64encode(image_file.read()).decode("utf-8") response = client.responses.create( model="gpt-5.5", input=[ { "role": "user", "content": [ {"type": "input_text", "text": prompt}, {"type": "input_image", "image_url": f"data:image/png;base64,{b64_image}"}, ], } ], )
Author   openai
🌐
Python documentation
docs.python.org › 3 › library › venv.html
venv — Creation of virtual environments
""" progress = self.progress while True: s = stream.readline() if not s: break if progress is not None: progress(s, context) else: if not self.verbose: sys.stderr.write('.') else: sys.stderr.write(s.decode('utf-8')) sys.stderr.flush() stream.close() def install_script(self, context, name, url): _, _, path, _, _ = urlsplit(url) fn = os.path.split(path)[-1] binpath = context.bin_path distpath = os.path.join(binpath, fn) # Download script into the virtual environment's binaries folder urlretrieve(url, distpath) progress = self.progress if self.verbose: term = '\n' else: term = '' if progress is n
🌐
Medium
medium.com › @agustinb › introduction-to-unicode-and-utf-8-in-python-9e7a844edddd
Introduction to Unicode and UTF-8 in Python 2 | by agustinb | Medium
December 1, 2018 - Python 2 does implicit decoding in order to make a single Unicode object, but its default codec is ASCII (you can run sys.getdefaultencoding() to check). So, in the first example, ‘world’ wasn’t a problem but ‘π’ cannot be decoded using ASCII. Codec is a shortcut for Encoder/Decoder. >>> content = '\xcf\x80-zza'.decode('utf-8') # π-zza>>> type(content) <type 'unicode'>>>> print content π-zza>>> output_string = content.encode('utf-8')>>> type(output_string) <type 'str'>>>> output_string '\xcf\x80-zza' # bytes!
🌐
LabEx
labex.io › tutorials › python-how-to-use-python-utf8-encoding-451217
How to use Python UTF8 encoding | LabEx
LabEx recommends mastering encoding techniques for robust text processing in Python. Working with text files requires careful handling of character encodings to ensure data integrity and compatibility. ## Reading files with specific encoding with open('example.txt', 'r', encoding='utf-8') as file: content = file.read() print(content) ## Writing files with UTF-8 encoding with open('output.txt', 'w', encoding='utf-8') as file: file.write("Python: 编程的魔力")
🌐
Evanjones
evanjones.ca › python-utf8.html
How to Use UTF-8 with Python (evanjones.ca)
For UTF-16, Python decoded the BOM into an empty string, but for UTF-8, it decoded it into a character. Why is there a difference? I think the UTF-8 decoder should do the same thing as the UTF-16 decoder and strip out the BOM.
🌐
Programiz
programiz.com › python-programming › methods › string › encode
Python String encode()
# print string print('The string is:', string) # default encoding to utf-8 · string_utf = string.encode() # print result print('The encoded version is:', string_utf) ... The string is: pythön! The encoded version (with ignore) is: b'pythn!' The encoded version (with replace) is: b'pyth?n!' Note: Try different encoding and error parameters as well. Since Python 3.0, strings are stored as Unicode, i.e.
🌐
Medium
lynn-kwong.medium.com › understand-the-encoding-decoding-of-python-strings-unicode-utf-8-f6f97a909ee0
Understand the encoding/decoding of Python strings (Unicode/UTF-8) | by Lynn G. Kwong | Medium
August 25, 2023 - We will introduce the basic concepts and usages of encoding and decoding in Python and how to generate unique hashes of strings with encoding and decoding.
🌐
Testsigma
testsigma.com › home › free tools › utf8 decode
UTF-8 Decoder Online | Free Decode & Validate Tool
Free UTF-8 decoder to convert hex, percent-encoded, binary & more. Validate bytes, detect errors, and inspect characters instantly no login needed.
🌐
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-python
UTF-8 in Python | Encoding Standards for Programming Languages
When you open a file for reading ("r") with a specified encoding, Python decodes the bytes from the file into strings using that encoding. Conversely, when writing ("w"), Python encodes your strings into bytes before saving them to the file, ...