Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Call str.encode() to convert Python text into bytes. For example, text.encode("utf-8") returns the text encoded as UTF-8. Choose the encoding expected by the file format, protocol, or API receiving the bytes; UTF-8 is a common choice when the destination supports it.
Convert a Python string with encode()
In Python 3, a str is Unicode text, while bytes is a sequence of encoded bytes. Encoding turns text into bytes using a specified character encoding, also called a codec.
text = "Hello, world!"
data = text.encode("utf-8")
print(data) # b'Hello, world!'
print(type(data)) # <class 'bytes'>
The displayed b'...' is Python’s representation of a bytes value; it does not mean the original text has become a different string. Python’s str.encode() defaults to UTF-8 if you omit the encoding, but specifying it makes the code’s intended format clear.
Choose the encoding the destination expects
Use the receiving system’s required encoding, not simply whichever encoding is most familiar. UTF-8 is widely used for interchange and can represent all Unicode code points. Plain ASCII text is valid UTF-8, but characters outside ASCII may take multiple bytes, so the byte count can exceed the character count.
#1 Best Overall
text = "café"
data = text.encode("utf-8")
restored = data.decode("utf-8")
assert restored == text
print(data) # b'cafxc3xa9'
The é character is represented by more than one byte in UTF-8. If a legacy interface specifically requires Latin-1, use that encoding instead; Latin-1 only maps code points U+0000 through U+00FF. The Python Software Foundation’s official Unicode HOWTO and codecs documentation describe these encoding behaviors and the importance of matching the format expected by the destination.
text = "café"
utf8_data = text.encode("utf-8")
latin1_data = text.encode("latin-1") # only when required by the destination
Handle characters an encoding cannot represent
By default, encoding uses the strict error policy: Python raises UnicodeEncodeError if the selected encoding cannot represent a character. For example, ASCII cannot encode é, and Latin-1 cannot encode characters outside its range.
Rank #2
text = "café"
text.encode("ascii") # raises UnicodeEncodeError
The errors argument can change how encoding failures are handled. For example, ignore omits unencodable characters and replace substitutes a replacement character. These strategies can discard or alter information, so use them only when that loss is acceptable to the application.
text.encode("ascii", errors="replace") # b'caf?'
Decode bytes with the matching encoding
To turn encoded bytes back into text, call decode() with the encoding used to create them—or the encoding declared by the source format. If the encoding is unknown, bytes cannot in general be reliably interpreted as the original text.
data = "café".encode("utf-8")
text = data.decode("utf-8")
Python does not automatically convert between str and bytes when you combine them. Encode or decode explicitly at the boundary where text and binary data meet; otherwise, operations that mix the types can raise TypeError.
Use text I/O for ordinary text files
If your goal is to read or write a text file, use Python’s text I/O and specify an encoding rather than manually encoding every value. Text I/O performs the necessary decoding when reading and encoding when writing.
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("café")
Use binary I/O when the application needs to work with bytes directly—for example, when handling a binary format or passing encoded data to an interface that explicitly expects bytes. The Python Software Foundation’s Unicode HOWTO recommends working with Unicode strings internally, decoding input as early as practical and encoding output at the end.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Do not use bytes(text) without an encoding
The bytes constructor requires an encoding when its input is a string. For normal string-to-bytes conversion, write text.encode("utf-8"), or provide the encoding explicitly to the constructor:
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteBest Value
text = "Hello"
data = bytes(text, encoding="utf-8")
A string of 0s and 1s is still text
If your string contains characters such as "01001000", encode() converts those characters into bytes; it does not interpret the digits as a binary number or automatically turn them into the corresponding words. To interpret a bit string, you need to define what those bits represent and how they are grouped and decoded.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




