Python String encode()

The string encode() method returns encoded version of the given string.

Since Python 3.0, strings are stored as Unicode, i.e. each character in the string is represented by a code point. So, each string is just a sequence of Unicode code points.

For efficient storage of these strings, the sequence of code points are converted into set of bytes. The process is known as encoding.

There are various encodings present which treats a string differently. The popular encodings being utf-8, ascii, etc.

Using string's encode() method, you can convert unicoded strings into any encodings supported by Python. By default, Python uses utf-8 encoding.


The syntax of encode() method is:

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

String encode() Parameters

By default, encode() method doesn't require any parameters.

It returns utf-8 encoded version of the string. In case of failure, it raises a UnicodeDecodeError exception.

However, it takes two parameters:

  • encoding - the encoding type a string has to be encoded to
  • errors - response when encoding fails. There are six types of error response
    • strict - default response which raises a UnicodeDecodeError exception on failure
    • ignore - ignores the unencodable unicode from the result
    • replace - replaces the unencodable unicode to a question mark ?
    • xmlcharrefreplace - inserts XML character reference instead of unencodable unicode
    • backslashreplace - inserts a \uNNNN espace sequence instead of unencodable unicode
    • namereplace - inserts a \N{...} escape sequence instead of unencodable unicode

Example 1: Encode to Default Utf-8 Encoding

# unicode string
string = 'pythön!'

# print string
print('The string is:', string)

# default encoding to utf-8
string_utf = string.encode()

# print result
print('The encoded version is:', string_utf)

When you run the program, the output will be:

The string is: pythön!
The encoded version is: b'pyth\xc3\xb6n!'

Example 2: Encoding with error parameter

# unicode string
string = 'pythön!'

# print string
print('The string is:', string)

# ignore error
print('The encoded version (with ignore) is:', string.encode("ascii", "ignore"))

# replace error
print('The encoded version (with replace) is:', string.encode("ascii", "replace"))

When you run the program, the output will be:

The string is: pythön!
The encoded version (with ignore) is: b'pythn!'
The encoded version (with replace) is: b'pyth?n!'

Note: Try different encoding and error parameters as well.