Python Encoding

Programming

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:
  1. strict – default response which raises a UnicodeDecodeError exception on failure
  2. ignore – ignores the unencodable unicode from the result
  3. replace – replaces the unencodable unicode to a question mark ?
  4. xmlcharrefreplace – inserts XML character reference instead of unencodable unicode
  5. backslashreplace – inserts a \uNNNN escape sequence instead of unencodable unicode
  6. 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)

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
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

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.

X-Soft

X-Soft is an established player in the IT market committed to providing excellent solutions for Web/ Mobile (iOS, Android, Web) around the globe. Namely, we are a team of professional web and mobile developers with 10+ years of experience.

Leave a Reply

Leave a Reply

Your email address will not be published.