Decode UTF-8 in Python

Vaibhav Vaibhav Dec 21, 2022
Decode UTF-8 in Python

Encoding refers to encoding a string using an encoding scheme such as UTF-8. Decoding refers to converting an encoded string from one encoding to another encoding scheme.

In this article, we will learn how to decode a string encoded in UTF-8 format in Python.

Decoding UTF-8 Strings in Python

To decode a string encoded in UTF-8 format, we can use the decode() method specified on strings.

This method accepts two arguments, encoding and error. encoding accepts the encoding of the string to be decoded, and error decides how to handle errors that arise during decoding.

The error argument accepts only two values: strict and ignore. strict raises a Unicode error when some error occurs, and ignore ignore the errors. The decode() method returns the original string.

Refer to the following Python code to understand how to use the decode() method.

s = "Hello World"
encoded = s.encode("UTF-8")
decoded = encoded.decode("UTF-8")
print("Encoded String:", encoded)
print("Decoded String:", decoded)

Output:

Encoded String: b'Hello World'
Decoded String: Hello World
Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.