how to encode and decode a string in python

String characters might be supported by the environment you are working in as well as your terminal but they might not be supported by all text editors or terminals. Another major concern is the security of data. It is necessary to keep encoded versions of strings to securely store passwords in the database. Data is usually transferred over the internet or network in the form of raw bytes. Non-ASCII characters cannot be represented by a single byte. Therefore, there should be some method to encode these strings.

To encode and decode a string, Python provides a built-in encode() method which allows us to convert a string into a specific format and the decode() method converts an encoded string back into original format. These methods are used to convert a string from one format to another specified format and vice versa.

In this tutorial, we will learn how to encode and decode a string in python using different methods and functions. If you want to learn more about Python Programming, visit Python Programming Tutorials.

String ENCODING Using encode() Method

In python 3.0, strings are usually stored as Unicode. In this encoding scheme, each character in the string is represented by a code point. To store these strings, these code points are usually converted into a set of bytes. This process is known as encoding. Using the string encode() method, you can convert unicode strings into any other supported encoding format. It uses utf8 encoding scheme by default.

The syntax of encode() function is:

string.encode(encoding, errors)

The encoding parameter specifies different encoding methods to encode a string such as utf-8, acsii etc. There are different methods available. The code to print all the available encodings is given below.

from encodings.aliases import aliases

# Display all the encoding methods available
print("The available encodings are : ")

Now, lets see how you can encode a string in ASCII format. Pass the input string to be encoded and the encoding format to the encode() function.

# initializing string
str = "entechin"

# printing the encoded string
print("The encoded string in ASCII format is : ", )
The encoded string in ASCII format is : 

The program raises a UnicodeDecodeError exception or error if the string encoding fails. We can deal with all such errors using the second argument of encode() method i.e., an error parameter.


The second argument “error” decides how to handle all errors encountered such as the UnicodeDecodeError discussed above. There are different types of error responses which are listed below:

  • strict – It is the default response which raises a UnicodeDecodeError exception on failure
  • ignore – ignores the unencodable unicode from the result
  • replace – replaces the unencodable unicode to a question mark ?
  • xmlcharrefreplace – inserts XML character reference instead of unencodable unicode
  • backslashreplace – inserts a \uNNNN escape sequence instead of unencodable unicode
  • namereplace – inserts a \N{…} escape sequence instead of unencodable unicode

The following example shows how you can encode a string using error parameter.

# unicode string
string = 'entechin'

# print string
print('The string is:', string)

# ignore error
print(string.encode("ascii", "ignore"))

# replace error
print(string.encode("ascii", "replace"))
The string is: entechin

String decoding using DECODE() FUNCTION

Decode() function is used to convert the encoded string back into the original form. It takes the encoded string as an input and return the original string. Like encode() function, it also uses error parameter for handling errors which arise as a result of a decode() method. First take an input string and encode the string into any format then decode the encoded string as shown in the example below. You’ll observe that it will return the original input string.

# initializing string
str = ""

# encoding string
str_enc = str.encode(encoding='utf8')

# printing the encoded string
print("The encoded string in base64 format is : ", )

# printing the original decoded string
print("The decoded string is : ", )
print(str_enc.decode('utf8', 'strict'))

The encoded string in base64 format is : 
The decoded string is :

application of encoding and decoding in communicaton

There are different fields where these methods can be used. One of them is cryptography which deals with the conversion of plain text into an unintelligible text for secure communication. This plain text can be a message or any confidential information such as passwords. Suppose you’re sending an email to a friend in which you have mentioned your account credentials. Lets build a simple code to encode and decode your credentials. If any hacker is able to hack your message, he/she would not be able to read the contents as they are in the ciphertext form. So this message would be of no use to him/her.

#Encoding password
import base64

login_ID= "entechin"
password = "1f2c56"



This encoded value shown in the output window above is sent in the email. When the email reaches the destination, it is decoded back into original form as shown below

import base64
#Your credentials that you want to protect
password = "MWYyYzU2"

#decode a message

#display the decoded value on a screen

Similarly, you can also encode and decode images. To see how images are encoded and decoded, visit this page. For communication, the messages must be converted into ciphertext before transmitting them to protect the message from hackers. The ciphertext is basically encoded text. This conversion is achieved using encoding technique. This message when reaches the destination, it should be restored into the original text known as decoding. This whole procedure is known as an Encryption-Decryption process. For more details about how this process can be achieved, visit this link.

In this tutorial, we have covered in detail the applications of encoding and decoding of a string and learnt how it can be done in python. If you’ve nay queries, let us know in the comments. Contact us.

Leave a Comment

Your email address will not be published.