Introduction to using the encode () method in Python, pythonencode
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:
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.
#! /Usr/bin/pythonstr = "this is string example... wow !!! "; Print" Encoded String: "+ str. encode ('base64', 'strict ')
When we run the above program, it will produce the following results:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE =