Python encode() Method

Python 字符串Python Strings


Description

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

Syntax

encode() method syntax:

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

Parameters

  • encoding -- The encoding to use, such as "UTF-8".
  • errors -- Sets the handling scheme for different errors. The default is 'strict', meaning that encoding errors cause a UnicodeError. Other possible values include 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace', and any value registered via codecs.register_error().

Return Value

This method returns the encoded string.

Example

The following example demonstrates the encode() method:

Example

#!/usr/bin/python

str = "this is string example....wow!!!";

print "Encoded String: " + str.encode('base64','strict')

The output of the above example is:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=

Python 字符串Python Strings

Other Extensions