Python 3 的文档:str.encode(encoding="utf-8", errors="strict")
Return an encoded version of the string as a bytes object. Default encoding is 'utf-8'. errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' and any other name registered via codecs.register_error(), see section Error Handlers. For a list of possible encodings, see section Standard Encodings.
Changed in version 3.1: Support for keyword arguments added.
Python 2.7 中:str.encode([encoding[, errors]])
Return an encoded version of the string. Default encoding is the current default string encoding. errors may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' and any other name registered via codecs.register_error(), see section Codec Base Classes. For a list of possible encodings, see section Standard Encodings.
New in version 2.0.
Changed in version 2.3:Support for 'xmlcharrefreplace' and 'backslashreplace' and other error handling schemes added.
Changed in version 2.7:Support for keyword arguments added.
encode 是用来把字符串转换成对应编码, 不加参数是默认 UTF-8. Python 2.7 返回的是 str, 3 变成了 byte array.