Python encode() Method

Definition
In Python, encode() converts a str into bytes using a character encoding such as UTF-8.
encoding and errors are optional. The defaults are UTF-8 and strict. Strict mode raises if a character cannot be encoded.
Syntax
Call encode on the string. Start with this line.
data = text.encode("utf-8")
| Call | What it does |
|---|
"Hello".encode() | UTF-8 bytes (default). |
"café".encode("ascii", errors="replace") | Replaces unencodable characters. |
encode() returns bytes. Use decode() on bytes to get text again.
Library declarations
str.encode(encoding="utf-8", errors="strict") → bytes
Parameters
| Parameter | Description |
|---|
| encoding | Optional. Character set name. Default is "utf-8". |
| errors | Optional. How to handle failures: "strict", "ignore", "replace", and others. |
Return Value
| Return Value | Description |
|---|
| bytes | The encoded binary data for the string. |
Python Examples
Example 1 — Basic encode()
Convert plain text to bytes with the default UTF-8 encoding.
text = "Python is amazing!"
data = text.encode()
print("Original:", text)
print("Encoded: ", data)
print("Type: ", type(data).__name__)
Original: Python is amazing!
Encoded: b'Python is amazing!'
Type: bytes
How It Works
- Start with str.
text is Unicode text. - Call encode(). No arguments → UTF-8.
- Get bytes. Result shows as
b'...'. - Original stays str. Only
data is the byte form.
Example 2 — Encoding Unicode Characters
Accented letters need more than one byte in UTF-8.
text = "café naïve"
data = text.encode("utf-8")
print("Text: ", text)
print("Bytes: ", data)
print("Chars: ", len(text))
print("Bytes#: ", len(data))
Text: café naïve
Bytes: b'caf\xc3\xa9 na\xc3\xafve'
Chars: 10
Bytes#: 12
How It Works
- Count characters. The string has 10 Unicode characters.
- Encode with UTF-8.
é and ï each become two bytes. - Byte length grows. Result is 12 bytes, not 10.
- Escapes in repr. Non-ASCII bytes print as
\x.. sequences.
Example 3 — The errors Parameter
ASCII cannot encode emoji—choose replace, ignore, or raise.
text = "Hello 🐍"
# strict would raise UnicodeEncodeError
safe = text.encode("ascii", errors="replace")
skip = text.encode("ascii", errors="ignore")
print("replace:", safe)
print("ignore: ", skip)
# round-trip with UTF-8
again = text.encode("utf-8").decode("utf-8")
print("round: ", again)
replace: b'Hello ?'
ignore: b'Hello '
round: Hello 🐍
How It Works
- ASCII is limited. The snake emoji has no ASCII mapping.
- replace. Unencodable chars become
?. - ignore. Those chars are dropped entirely.
- UTF-8 round-trip. Encode then
decode with the same codec restores the text.
Frequently Asked Questions
It converts a str into bytes using a character encoding.
A bytes object, not a str.
UTF-8 in Python 3.
It controls failures. strict raises UnicodeEncodeError by default.
encode() is str → bytes. decode() is bytes → str.
Next: endswith()
Test whether a string ends with a given suffix.
endswith() →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful