The encode() method does not require any parameters by default.
It returns a string that have encoded in utf-8. It throws a UnicodeDecodeError exception if it fails.
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
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
# print result
print('The encoded version is:', string_utf)
Output
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
# replace error
String Encoding
In general, strings have been stored as Unicode since Python 3.0, which means that each character in the string is represented by a code point. As a result, each string is simply a series of Unicode code points.
So,the sequence of code points is transformed into a series of bytes for efficient storing of these strings. Encoding is the term for this procedure.
The most common encodings are utf-8, ascii, and so on.
Hence, using the string encode()
method, you can convert unicode strings into any encodings supported by Python. By default, Python uses utf-8 encoding.