Python encode() method
Description
The Python encode() method encodes a string using encoding the specified encoding. The errors parameter can specify different error-handling schemes.
Syntax
Syntax of the encode() method:
str.encode(encoding='UTF-8',errors='strict')
Parameters
- encoding -- the encoding to use, such as "UTF-8".
- errors -- sets different error handling schemes. Defaults to 'strict', meaning an encoding error raises a UnicodeError. Other possible values are 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace', and any value registered through 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!!!"
;
"Encoded String: "
str
.
encode
(
'base64'
,
'strict'
)
The output of the above example is:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
Python Strings