Easy Tutorial
❮ Ref Set Symmetric_Difference_Update Python Att Time Mktime Html ❯

Python3 encode() Method

Python3 Strings


Description

The encode() method encodes the string in the specified encoding format. The errors parameter can specify different error handling schemes.

Syntax

Syntax for the encode() method:

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

Parameters

Return Value

This method returns the encoded string, which is a bytes object.

Example

The following example demonstrates the encode() method:

Example (Python 3.0+)

#!/usr/bin/python3

str = "tutorialpro.org";
str_utf8 = str.encode("UTF-8")
str_gbk = str.encode("GBK")

print(str)

print("UTF-8 Encoding:", str_utf8)
print("GBK Encoding:", str_gbk)

print("UTF-8 Decoding:", str_utf8.decode('UTF-8', 'strict'))
print("GBK Decoding:", str_gbk.decode('GBK', 'strict'))

The output of the above example is as follows:

tutorialpro.org
UTF-8 Encoding: b'\xe8\x8f\x9c\xe9\xb8\x9f\xe6\x95\x99\xe7\xa8\x8b'
GBK Encoding: b'\xb2\xcb\xc4\xf1\xbd\xcc\xb3\xcc'
UTF-8 Decoding: tutorialpro.org
GBK Decoding: tutorialpro.org

Python3 Strings

❮ Ref Set Symmetric_Difference_Update Python Att Time Mktime Html ❯