Python encode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

python
data = text.encode("utf-8")
CallWhat 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
python
str.encode(encoding="utf-8", errors="strict") → bytes

Parameters

ParameterDescription
encodingOptional. Character set name. Default is "utf-8".
errorsOptional. How to handle failures: "strict", "ignore", "replace", and others.

Return Value

Return ValueDescription
bytesThe encoded binary data for the string.

Python Examples

Example 1 — Basic encode()

Convert plain text to bytes with the default UTF-8 encoding.

python
text = "Python is amazing!"
data = text.encode()

print("Original:", text)
print("Encoded: ", data)
print("Type:    ", type(data).__name__)

How It Works

  1. Start with str. text is Unicode text.
  2. Call encode(). No arguments → UTF-8.
  3. Get bytes. Result shows as b'...'.
  4. Original stays str. Only data is the byte form.

Example 2 — Encoding Unicode Characters

Accented letters need more than one byte in UTF-8.

python
text = "café naïve"
data = text.encode("utf-8")

print("Text:  ", text)
print("Bytes: ", data)
print("Chars: ", len(text))
print("Bytes#: ", len(data))

How It Works

  1. Count characters. The string has 10 Unicode characters.
  2. Encode with UTF-8. é and ï each become two bytes.
  3. Byte length grows. Result is 12 bytes, not 10.
  4. Escapes in repr. Non-ASCII bytes print as \x.. sequences.

Example 3 — The errors Parameter

ASCII cannot encode emoji—choose replace, ignore, or raise.

python
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)

How It Works

  1. ASCII is limited. The snake emoji has no ASCII mapping.
  2. replace. Unencodable chars become ?.
  3. ignore. Those chars are dropped entirely.
  4. 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() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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