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.

encode() Syntax

The syntax of encode() method is:

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

encode() 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.

 encode() Return Value

The encode() function returns the encoded version of a string as a 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 it 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

Get notified of the best deals on our WordPress themes.

You May Also Like
List of Python free ebooks

List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…
View Post