Introduction to the encode () method in Python
This article describes how to use the encode () method in Python. It is a basic knowledge in Python. For more information, see
The encode () method returns the encoding version of the string. The default encoding is the current default string encoding. You can set different error handling mechanisms.
Syntax
The following is the syntax of the encode () method:
?
|
1 |
Str. encode (encoding = 'utf-8', errors = 'strict ')
|
Parameters
Encoding -- this is the encoding used. For a list of all encoding schemes, visit: standard encoding library
Errors -- this may be a different error handling mechanism. The default error is "strict", that is, the encoding error is UnicodeError. Other possible values are ignore ', 'replace', 'xmlcharrefresh', and 'backslashreplace 'and any other names registered through codecs. register_error.
Return Value
The encoding version of the string returned by this method.
Example
The following example shows how to use the encode () method.
?
|
1
2
3
4
5 |
#! /Usr/bin/python
Str = "this is string example... wow !!! ";
Print "Encoded String:" + str. encode ('base64', 'strict ')
|
When we run the above program, it will produce the following results:
?
|
1 |
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE =
|