Python String encode()

In this tutorial, we will learn about the Python String encode() method with the help of examples.

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

Example

title = 'Python Programming'

# change encoding to utf-8
print(title.encode())

# Output: b'Python Programming'

1. Syntax of String encode()

The syntax of encode() method is:

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

2. String encode() Parameters

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

It returns an 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 escape sequence instead of unencodable unicode
    • namereplace – inserts a \N{…} escape sequence instead of unencodable unicode

3. 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)

Output

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

4. 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"))

Output

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.

5. String Encoding

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 is converted into a set of bytes. The process is known as encoding.

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

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