Python3 string.encode()方法

encode()方法返回字串的編碼版本。默認編碼是當前的默認字串編碼。錯誤可以給定設置一個不同的錯誤處理方案。

語法

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

參數

  • encoding -- 這是要使用的編碼。對於所有的編碼方案的列表,請訪問: 標準編碼

  • errors -- 這是給出設置一個不同的錯誤處理方案。默認的錯誤是“strict”,即編碼錯誤引發一個UnicodeError。

    其他可能的值是 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 並通過codecs.register_error()註冊的其他名稱

返回值

字串解碼

示例

#!/usr/bin/python3

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

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

結束

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=

上一篇: Python3數字 下一篇: Python3字串