YouTip LogoYouTip

Python3 String Decode

# Python3.x Python3 bytes.decode() Method [![Image 3: Python3 Strings](#) Python3 Strings](#) * * * ## Description The decode() method decodes a bytes object using the specified encoding. The default encoding is 'utf-8'. ## Syntax The syntax for the decode() method is: bytes.decode(encoding="utf-8", errors="strict") ## Parameters * encoding -- The encoding to use, such as "UTF-8". * errors -- Sets the error handling scheme. The default is 'strict', which means that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace', and any other value registered with codecs.register_error(). ## Return Value This method returns the decoded string. ## Example The following examples demonstrate the use of the decode() method: ## Example (Python 3.0+) #!/usr/bin/python3 str = ""; str_utf8 = str.encode("UTF-8")str_gbk = str.encode("GBK")print(str)print("UTF-8 Encoding:", str_utf8)print("GBK Encoding:", str_gbk)print("UTF-8 Decoding:", str_utf8.decode('UTF-8','strict'))print("GBK Decoding:", str_gbk.decode('GBK','strict')) The output of the above example is as follows: UTF-8 Encoding: b'xe8x8fx9cxe9xb8x9fxe6x95x99xe7xa8x8b' GBK Encoding: b'xb2xcbxc4xf1xbdxccxb3xcc' UTF-8 Decoding: GBK Decoding: * * Python3 Strings](#)
← Python3 String EndswithPython3 String Center β†’