Python 3 – String decode() Method
In Python 3, the
“`decode()“` method is used to convert an encoded string into a Unicode character string. The method is a part of the string class and can be used with any string object. In this tutorial, we will learn how to use the “`decode()“` method in Python 3 with some sample codes.
Syntax
The syntax of the
“`decode()“` method is as shown below:
string.decode(encoding=encoding, errors=errors)
Here,
*
“`string“` is the string to be decoded
* “`encoding“` is the encoding type to be used (optional)
* “`errors“` is the error handling scheme to use (optional)
Parameters
The
“`decode()“` method in Python 3 accepts two optional arguments:
* “`encoding“` – This parameter specifies the encoding type to be used while decoding the string.
* “`errors“` – This parameter specifies how the errors should be handled. By default, it is set to “strict” which means the method raises an error if there is any decoding error.
Working with the
“`decode()“` Method
Here is an example of using the
“`decode()“` method in Python 3:
string = "Hello! This is an encoded string."
# perform encoding
encoded_string = string.encode('utf-8')
print("Encoded string: ", encoded_string)
# perform decoding
decoded_string = encoded_string.decode('utf-8')
print("Decoded string: ", decoded_string)
In the above code, we have created a string and then performed encoding using the
“`encode()“` method. The encoding used here is UTF-8. After encoding, we have printed the encoded string. Next, we have performed decoding using the “`decode()“` method using the same encoding type, and printed the decoded string. The respective output for the above code is:
Encoded string: b'Hello! This is an encoded string.'
Decoded string: Hello! This is an encoded string.
Encoding Types
There are various encoding types that can be used with the
“`decode()“` method in Python 3. Here is a list of some popular encoding types:
* ASCII
* UTF-8
* UTF-16
* ISO-8859-1
* Latin-1
* CP1252
* CP437
To work with different encoding types, just use the encoding type accordingly in the
“`decode()“` method.
Handling Errors
The
“`decode()“` method in Python 3 also provides an optional “`errors“` parameter, which can be used to specify how the errors should be handled while decoding the string. There are three options available for handling errors:
* “`strict“` (default) – raises a UnicodeDecodeError on failure
* “`ignore“` – ignores any decoding error and simply keeps decoding the remaining parts of the string
* “`replace“` – replaces any error with a specified replacement character.
Here is an example of using error handling with the
“`decode()“` method in Python 3:
string = "Hello! This is an encoded string."
# perform encoding
encoded_string = string.encode('cp1252')
print("Encoded string: ", encoded_string)
# replace and ignore error handling
print("Decoded string (replace): ", encoded_string.decode('ascii', 'replace'))
print("Decoded string (ignore): ", encoded_string.decode('ascii', 'ignore'))
In the above code, we have encoded the string using CP1252 encoding. We have then printed the encoded string. Next, we have used the error handling scheme ‘replace’ which replaces any error with a specified replacement character. We have also used the error handling scheme ‘ignore’ which ignores any decoding error and simply keeps decoding the remaining parts of the string.
The respective output for the above code is:
Encoded string: b'Hello! This is an encoded string.'
Decoded string (replace): Hello! This is an encoded string.
Decoded string (ignore): Hello! This is an encoded string.
Conclusion
In this tutorial, we have discussed how to use the
“`decode()“` method in Python 3 to convert an encoded string into a Unicode character string. We have also looked at how to work with different encoding types and handle errors while decoding the string. With this knowledge, you can now use the “`decode()“` method effectively in any Python 3 project!