Python Tutorial - Encode and Decode Function

Python Exercise Lists

How to conduct string encoding conversion in Python?

We know that the earliest string encoding was ASCII encoding, which encodes only 10 numbers, 26 uppercase and lowercase English letters, and some special characters. ASCII code can only represent 256 symbols long, and each character only takes 1 byte.

With the development of information technology, the characters of various countries need to be encoded, so GBK, GB23112, UTF-8 encoding, etc. have appeared successively, which requires that the English character mother occupies 1 byte , Chinese characters occupy 2 bytes; UTF-8 is an internationally adopted encoding format, which contains characters needed by all countries in the world. It stipulates that English characters occupy 1 byte and Chinese characters occupy 3 bytes.

In Python, there are two commonly used string types, str and bytes, where str is used to represent Unicode characters and bytes is used to represent binary data. The str and bytes types need to be converted using encode() and decode() functions.

Python Encode Function

The encode() function provides methods for the string type (str) to convert the str type to the bytes type. This process is also called "encoding".

The syntax of the encode() function is as follows:

str.encode([encoding=”utf-8”][errors=”strict”])

Note that the parameters enclosed in [] in the format are optional parameters, that is, when using this function, the parameters in [] can be used or not.

The meaning of each parameter of this method is shown in the following table:

Parameter Meaning
str Represents a string to be converted.
encoding = “utf-8”

Specifies the character encoding used for encoding. This option defaults to UTF-8 encoding.

When only one parameter is used in the method, you can omit the preceding "encoding =" and write the encoding format directly, such as str.encode ("UTF-8").

errors = “strict”

Specifies the error handling method. The selectable values are:

  • strict: throws an exception when it encounters an illegal character.
  • ignore: Ignore illegal characters.
  • replace: Replace illegal characters with "?"
  • xmlcharrefreplace: Character reference using xml.

The default value of this parameter is strict.

Note that using the encode () method to encode the original string will not directly modify the original string. If you want to modify the original string, you need to re-assign.

[Example:] The string type string "Python Language Website" is converted to bytes type.

string = "Python Language Website"
string.encode()

The output is:

b'Python Language Website'

Python Tutorial - Decode Function

In contrast to the encode() function, the decode() function is used to convert binary data of type bytes to str. This process is also called "decoding".

The syntax of the decode () method is as follows:

bytes.decode([encoding="utf-8”][errors=”strict”])

The meanings of the parameters in this method are shown in the following table.

Parameter Meaning
bytes Represents binary data to be converted.
encoding = “utf-8”

Specifies the character encoding used during decoding. The default is UTF-8. When only one parameter is used in the method, you can omit "encoding =" and write the encoding directly.

Note that when decoding bytes type data, you must choose the same format as when you originally encoded it.

errors = “strict”

Specifies the error handling method. The selectable values are:

  • strict: throws an exception when it encounters an illegal character.
  • ignore: Ignore illegal characters.
  • replace: Replace illegal characters with "?"
  • xmlcharrefreplace: Character reference using xml.

The default value of this parameter is strict.

[Example:]

string = "Python Language Website"
string.decode()

The output is:

---------------------------------------------------------------------------
AttributeError             Traceback (most recent call last)
[ipython-input-2-8e0054fd2199] in [module]
1 string = "Python Language Website"
----> 2 string.decode() AttributeError: 'str' object has no attribute 'decode'

Note that if the encoding is not the default UTF-8 encoding, you must choose the same format as when

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises