Python

Python String Decode Method

The Python language is used to store the string in the form of Unicode. Within Unicode, a simple code point is utilized to represent a single character of a Unicode. We have to know two terms: encode and decode. The encoding would convert a simple string to a group of bytes while decoding will convert the group of bytes to a real string once again.

So, within this article today, we will be decoding a string to an original one with the encode() and decode() function. Be sure to configure the python3 package on your Linux system. Let’s start today’s article by launching the terminal console using the Ctrl+Alt+T.

Example 1

We will be starting the first example within the python3 console of the Ubuntu 20.04 shell terminal. So, we have started it with the keyword Python3 as shown in the output beneath.

$ python3

The console is now ready to be used. So, we have initialized a string variable named “s” and assigned it some value. Its value contains a mix of integers that are being converted into a character type and concatenated with a string type value “hello”. On the next line, we have initialized another variable named “enc”.

The encode() method has been used here to encode the original variable “s” to utf-8 encoding and saved the encoded string to a variable “enc”. The next consecutive line is using a print clause to print the encoded string value i.e. “enc”. The terminal shows the encoded string in bytes. The script that is explained above is cited here.

>>> s = chr(13) + ‘hello’ + chr(14)
>>> enc = s.encode( ‘utf-8)
>>> print(enc)
b’\rhello\x0e’

It’s time to decode back the encoded string to its original form. So, we have applied the decode function on the variable “enc” to convert it back to the original string and save it to the variable “dec”. The print statement has been executed to print the decoded string on the shell as shown in the image below i.e., hello. The script that is explained above is cited here.

>>> = enc.decode()
>>> print(dec)
hello

Example 2

Let’s take another example to decode a string. We have created a new Python type file. After adding the Python support, we have initialized a string “str” and encoded it to utf-8 type byte format using the encode function. The errors are set to “strict” to raise only a UnicodeError and the rest will be ignored.

The encoded string will be saved to the variable “enc” and the print clause will print the type of encoded variable using the “type()” method. The print statement will print out the encoded string and the decode function will decode it back to the original one. The decoded string will be printed out.  The script that is explained above is cited here.

#!/usr/bin/python3
str = “HelloLinux”
enc = str.encode(‘utf-8, ‘strict’)
print(type(enc))
print(“The encoded string: ”, enc)
dec = enc.decode(‘utf-8, ‘strict’)
print(“The decoded string: ”, dec)

Execution of this Python file displays the type of encoded string i.e., bytes and shows the encoded and decoded string separately.

$ python3 decode.py

Example 3

Let’s end this article with the last example. This time we will be converting our string to utf_16 format of bytes. So, we have initialized a string and encoded it to utf_16 encoding using the encode() function on it.

The encoded string has been saved to variable “enc” and we have printed its type and value. The encoded string variable has been decoded into an original one with the use of the decode() function on the “enc” variable and printed out on the shell. . The script that is explained above is cited here.

#!/usr/bin/python3
str = “HelloLinux”
enc = str.encode(“utf-16)
print(type(enc))
print(“The encoded string: ”, enc)
dec = enc.decode(‘utf-16, ‘strict’)
print(“The decoded string: ”, dec)

After running this updated code of Python with the python3 keyword, we have got the display of encoded string type as “bytes” along with the encoded and decoded string.

$ python3 decode.py

Conclusion

Within this article, we have demonstrated simple examples to decode an encoded string back to the original one. We have encoded the simple strings to utf-8 and utf-16 bytes formats and then decode them back to the original string. We hope it will be helpful.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content