Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsUse str.encode() to turn Python text into bytes. For a mutable byte array, wrap the result in bytearray:
text = "café"
data = text.encode("utf-8") # bytes
mutable_data = bytearray(data) # bytearray
values = list(data) # list of integers
Choose the output type your code needs, and specify the text encoding—usually UTF-8—so the bytes can be interpreted correctly.
Choose the result type you need
Python str objects hold text; bytes and bytearray hold encoded binary data. Encoding is the step that represents text as bytes. Python’s built-in type documentation describes these types and str.encode().
| Result | How to create it | What it is for |
|---|---|---|
bytes |
text.encode("utf-8") |
Immutable binary data; the usual choice for files, network protocols, and APIs that accept bytes. |
bytearray |
bytearray(text.encode("utf-8")) |
Mutable binary data when bytes need to be changed in place. |
list of integers |
list(text.encode("utf-8")) |
One integer from 0 through 255 for each encoded byte; use when an interface specifically expects integer values. |
In Python, “byte array” can mean a mutable bytearray, but some callers use it loosely to mean bytes in general. Check what the receiving function requires: bytes, bytearray, and a list of integers are different types.
Recommended Free Tools
#1 Best Overall
Encode a string as bytes
Call encode() on the string. UTF-8 is Python’s default encoding for this method, but stating it explicitly makes the intended representation clear:
text = "Hello, 世界"
encoded = text.encode("utf-8")
print(type(encoded)) # <class 'bytes'>
UTF-8 can represent every Unicode code point. ASCII characters use one byte, while many other characters use more. As a result, the number of encoded bytes may differ from the number of characters in the string. See the Python Unicode HOWTO for an explanation of Unicode and UTF-8.
Rank #2
Make the bytes mutable or inspect their values
Convert to a mutable bytearray
Pass the encoded bytes to bytearray() when you need to edit the binary data:
text = "Hello, 世界"
mutable = bytearray(text.encode("utf-8"))
mutable[0] = ord("h")
The original string is unchanged; the assignment changes the first byte in the mutable array.
Get one integer per byte
Use list() if an API or debugging task needs integer byte values:
encoded = "Aé".encode("utf-8")
values = list(encoded)
print(values) # [65, 195, 169]
The two displayed characters produce three UTF-8 bytes, so the list has three values. A list is not itself a byte buffer and is usually unnecessary when the consumer accepts bytes or bytearray.
Choose an encoding that matches the receiver
Use UTF-8 for general text interchange unless a file format, API, or legacy protocol requires another encoding. If a specification requires a legacy encoding, name it rather than relying on an environment-dependent assumption. For example, Latin-1 maps code points U+0000 through U+00FF; a character outside that range cannot be encoded with it under strict handling. Python’s codecs documentation describes encodings and UTF-8 variants.
text = "café"
encoded = text.encode("latin-1")
The default error policy is strict: if the selected encoding cannot represent a character, Python raises UnicodeEncodeError. You can request errors="ignore" or errors="replace", but these policies drop or substitute unrepresentable text. Use them only when that change is acceptable for the data.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Best Value
Decode bytes back into text
To recover text, decode the bytes using the same encoding used to create them:
text = "Hello, 世界"
encoded = text.encode("utf-8")
restored = encoded.decode("utf-8")
str(encoded) is not a substitute for decoding. It gives a representation of the bytes object rather than interpreting those bytes as text.
UTF-8 and Base64 are different operations
UTF-8 encodes Unicode text as bytes. Base64 takes existing binary data and represents it using printable ASCII characters. Base64 does not choose how text should be encoded; if you need Base64 text, first encode the string with the character encoding required by your format, then apply Base64 to those bytes.
Use a UTF-8 BOM only when required
Ordinary UTF-8 does not require a byte-order mark (BOM). Python’s utf-8-sig variant writes a BOM when encoding and skips it at the start when decoding. Use that variant only when the receiving file format or application expects the signature.
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.




