How to use encode() Function in Python with Example?

The `encode()` function in Python is used to convert a string into a specified encoding format. This function returns the encoded version of the string as a bytes object. It’s commonly used when dealing with text data that needs to be converted to bytes for storage or transmission.

Syntax

string.encode(encoding='utf-8', errors='strict')

encoding: The encoding format in which the string is to be encoded. The default is `’utf-8’`.
errors: Specifies how to handle encoding errors. The default is `’strict’`, which raises a UnicodeEncodeError. Other options include `’ignore’`, `’replace’`, `’xmlcharrefreplace’`, `’backslashreplace’`, etc.

Example 1: Basic Usage

text = "Hello, World!"
encoded_text = text.encode() # Uses default 'utf-8' encoding
print(encoded_text) # Output: b'Hello, World!'

Example 2: Encoding with Different Format

text = "Hello, World!"
encoded_text = text.encode('ascii') # Encodes using ASCII encoding
print(encoded_text) # Output: b'Hello, World!'

Example 3: Handling Errors

text = "Hello, World!"
try:
encoded_text = text.encode('ascii')
except UnicodeEncodeError as e:
print("Encoding Error:", e) # Output: 'ascii' codec can't encode character '\U0001f600' in position 13: ordinal not in range(128)

# Using 'replace' to handle errors
encoded_text = text.encode('ascii', 'replace')
print(encoded_text) # Output: b'Hello, World! ?'

In this example, the `encode()` function tries to encode a string containing a Unicode character (a smiley face) using ASCII encoding, which cannot represent the smiley face. The `’replace’` option replaces the unencodable character with a question mark (`?`).