English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python string encode() usage and example

Python string methods

The string encode() method encodes the string using the specified encoding. If no encoding is specified, UTF-8.

from Python 3starting fromStringstored in Unicode format, that isStringEach character is represented by a code point. Therefore, each string is just a sequence of Unicode code points.

To effectively store these strings, convert the code point sequence to a byte set. This process is calledEncoding.

There are various different encodings, which handle strings differently. Popular encodings areutf-8,asciietc.

Using the encode() method of the string, you can convert an unencoded string to any encoding supported by Python. By default, Python usesutf-8Encoding.

The syntax of the encode() method is:

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

String encode() parameter

By default, the encode() method does not require any parameters.

It returns the UTF string-8Encoding version. If an error occurs, it will raise a UnicodeDecodeError exception.

But it needs two parameters:

  • encoding -The encoding type must be encoded as a string

  • errors-Response when encoding fails. There are six types of error responses

    • strict-Default response, which will raise a UnicodeDecodeError exception when it fails

    • ignore-Ignore unencodable unicode from the result

    • replace-Replace unencodable Unicode with question mark?

    • xmlcharrefreplace-Insert XML character references instead of unencodable unicode

    • Backslash replacement-Insert \ uNNNN space sequence instead of unencodable unicode

    • namereplace-Insert \ N {...} escape sequence instead of unencodable unicode

Example1: Encoding is the default Utf-8Encoding

# Unicode string
string = 'pythön!'
# Output string
print('String:', string)
# Default encoding is utf-8
string_utf = string.encode()
# Output result
print('Encoding version is:', string_utf)

When running the program, the output is:

String: pythön!
Encoding version is: b'pyth\xc3\xb6n!"

Example2Use incorrect encoding parameters

# Unicode string
string = 'pythön!'
# Output string
print('String:', string)
# ignore error
print('Encoded version (ignore):', string.encode("ascii", "ignore"))
# Replace error
print('Encoded version (replace):', string.encode("ascii", "replace"))

When running the program, the output is:

String: pythön!
Encoded version (ignore) : b'pythn!'
Encoded version (replace) : b'pyth?n!'

Note:Try different encoding and error parameters.

Python string methods