Python String encode()

Python String encode() method is a built-in function that takes an encoding type as an argument and returns the encoded version of a string as a bytes object. The default encoding is UTF-8 if no arguments are passed.

Syntax

The syntax of encode() method is:

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

Parameter

The encode() function can take two parameters, and both are optional.

  • encoding (optional) – The encoding type in which the string needs to be encoded.
  • errors (optional) –  Decides how to handle the error if the encoding fails. There are seven types of an error responses.
    • strict – default response, which raises a UnicodeDecodeError exception on failure
    • ignore – ignores the unencodable Unicode from the result
    • replace – Replace with a suitable replacement marker; Python will use the official U+FFFD REPLACEMENT CHARACTER for the built-in codecs on decoding, and ‘?’ on encoding.
    • xmlcharrefreplace – replaces with the appropriate XML character reference instead of unencodable Unicode
    • backslashreplace – Replace with backslashed escape sequences instead of unencodable Unicode
    • namereplace – replaces with \N{…} escape sequences instead of unencodable Unicode
    • surrogateescape – On decoding, replace byte with individual surrogate code ranging from U+DC80 to U+DCFF. This code will then be turned back into the same byte when the 'surrogateescape' error handler is used when encoding the data.

Return Value

The encode() function returns the encoded version of a string as bytes object.

Example 1- Encode the string to Utf-8 Encoding

text = "Hellö Wörld"

print("The Original String is:", text)
# default encode is UTF-8
encoded_str = text.encode()
print("The UTF-8 Encoded String is:", encoded_str)

Output

The Original String is: Hell� W�rld
The UTF-8 Encoded String is: b'Hell\xc3\xb6 W\xc3\xb6rld'

Example 2- UnicodeEncodeError while encoding string

text = "Hellö Wörld"

print("The Original String is:", text)
# encode to ascii
encoded_str = text.encode("ascii")
print("The ascii encoded String is:", encoded_str)

Output

The Original String is: Hell� W�rld
Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 5, in <module>
    encoded_str = text.encode("ascii")
UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 4: ordinal not in range(128)

Example 3- Handling Encoding errors with error parameters

In case of any errors during encoding, we can handle them by passing the error argument. The default value is strict but we can specify other values such as  and allows other possible values ‘ignore’, ‘replace’, ‘xmlcharrefreplace’, ‘backslashreplace’ etc 

text = "Hellö Wörld"

print("The Original String is:", text)

# encodes into ascii and ignores any errors while encoding
encoded_str1 = text.encode("ascii","ignore")

# encodes into ascii and replaces the characters with ? 
encoded_str2 = text.encode("ascii","replace")

print("The ascii encoded String is:", encoded_str1)
print("The ascii encoded String is:", encoded_str2)

Output

The Original String is: Hell� W�rld
The ascii encoded String is: b'Hell Wrld'
The ascii encoded String is: b'Hell? W?rld'
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy.repeat() Function

numpy.repeat() Function

Table of Contents Hide SyntaxRepeat ParametersReturn ValueExample 1: Repeating the Single-Dimensional NumPy ArrayExample 2: Repeating the Two-Dimensional NumPy ArrayExample 3: Repeating the NumPy Array Row WiseExample 4: Repeating the NumPy…
View Post