How to do Base64 Encoding and Decoding in Python?

If you wish to transmit your secret data from one end to another, then an option that you might consider is to encode it using the Base64 coding. On the other end, you will have to decode that data again using the Base64 decoding. Only then will you be able to extract your useful data wherever it is really needed. Moreover, at times, you might just want to secure your data somewhere by keeping it in an encoded format and then decode it as needed. By now, you would have got the basic gist of this article. Yes, we want to write about the Base64 encoding and decoding; however, we will be doing this in reference to the Python programming language. Nevertheless, before proceeding with that, we first need to justify the reason for choosing this language for performing the Base64 encoding and decoding, and for that, you will have to read the next section of this article.

Why is Python so Popular amongst the Developers’ Community?

Python has a very simple syntax that the users manage to learn very quickly. This simplicity in its syntax makes the code written in this programming language very easy to execute. Python has very resourceful community support, which makes it all the more usable. Apart from that, this programming language also offers different data science and analytics tools whose need is increasing day by day. All of these reasons make Python extremely popular among the developers’ community. This is also the reason why we choose this programming language for performing the Base64 encoding and decoding in this article. Now, let us go through the crux of this article, i.e., the Base64 encoding and decoding in Python in Ubuntu 20.04.

How to Accomplish Base64 Encoding and Decoding in Python in Ubuntu 20.04?

The Base64 encoding and decoding can be performed very easily in Ubuntu 20.04 while using the Python programming language. To explain to you, we have designed the following Python script. If we talk briefly about this Python script, then for encoding, it will first take a sample string and convert it into the respective ASCII code. After that, it will encode this ASCII code and then decode the encoded ASCII code into the corresponding encoded string. In the same manner, the complete reverse of this process will be followed for decoding. You will be capable of understanding all this in a better way by going through the exact script that we have written.

We have first created an empty document in the Home directory of our Ubuntu 20.04 system and have named it “” In this Python script file, we have imported the “base64” library so that we can conveniently encode and decode our desired text. After that, we have defined a string named “text” and have assigned to it the string “This is a sample string.” Then, we printed this string on the terminal so that we could later compare it with the decoded string.

We then created a variable called “text_bytes” for converting our original string into ASCII code. Then, we created another variable, “encoded_bytes,” to hold the encoded text bytes with Base64. After that, we decoded the “encoded_bytes” from ASCII code to normal encoded string text. Then, we simply printed the encoded text on the terminal.

For decoding the encoded string, we have created a variable “base64_bytes” and have assigned to it the encoded text after converting it to the ASCII format. Then, we created another variable, “decoded_bytes,” and assigned to it the decoded Base64 bytes. After that, we have created a variable “decoded_text” to hold the decoded string after it is converted back to the normal format from the ASCII format. Finally, we have used the last “print” statement for printing the decoded string on the terminal.

Ensuring the Presence of Python on Ubuntu 20.04

Now, we need to confirm that Python is installed on our Ubuntu 20.04 system because otherwise, we will not be able to execute our Python script. Since Python3 is the latest available version of Python, therefore, we have installed it on our Ubuntu 20.04 system already (you can also have any other version of your choice installed on your system). To confirm its presence on our system, we have executed the command shown beneath:

$ python3 --version

The version of Python3 installed on our Ubuntu 20.04 system is shown in the following image. This shows that Python3 is present in our system.

Executing the Python Encoding and Decoding Script in Ubuntu 20.04

A Python script can be executed instantly on a Ubuntu 20.04 system if you have Python installed on it. You just need to execute the command listed below:

$ python3

If you have a version of Python other than Python3 on your system, then you will be able to execute it only with the “python” keyword. Moreover, you will have to write the name of your specific Python script file to be executed, followed by the “.py” extension.

Result of the Python Base64 Encoding and Decoding Script

The result of the Python script that we have created above is shown in the following image:

You can easily visualize from this result that first, our script printed the original string on the terminal, followed by the encoded string, and then the decoded string. You can also verify that the decoded string is exactly the same as the original string. It implies that the Python3 script that we have written above has correctly encoded and decoded our data with Base64 in Ubuntu 20.04.


By designing this guide, we wanted to teach our readers the process of performing the Base64 encoding and decoding in Python in Ubuntu 20.04. For that, we first justified Python’s popularity among the developers, followed by the actual Python script for the Base64 encoding and decoding. After that, we thoroughly discussed the result of this script with you. By using this guide as a baseline, you will be able to encode and decode your desired data with Base64 in Ubuntu 20.04 very conveniently.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.