Open In App
Related Articles

Python Strings decode() method

Improve Article
Save Article
Like Article

In Python we have decode() is a method specified in Strings. This method is used to convert from one encoding scheme, in which the argument string is encoded to the desired encoding scheme. This works opposite to the encode. It accepts the encoding of the encoding string to decode it and returns the original string.

Python Decode() Function Syntax

Syntax: decode(encoding, error)

  • encoding : Specifies the encoding on the basis of which decoding has to be performed. 
  • error : Decides how to handle the errors if they occur, e.g ‘strict’ raises Unicode error in case of exception and ‘ignore’ ignores the errors occurred. 
  • Returns : Returns the original string from the encoded string.

Encode and Decode a String in Python

The above code is an example of encoding and decoding. Here first we encoded the string using UTF-8 and then decoded it which gives the same output String as we give it in input.


# initializing string
String = "geeksforgeeks"
encoded_string = String.encode('utf-8')
print('The encoded string in base64 format is :')
decoded_string = encoded_string.decode('utf-8')
print('The decoded string is :')


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

Application of Encode-Decode

Encoding and decoding together can be used in the simple applications of storing passwords in the back end and many other applications like cryptography which deals with keeping information confidential. A small demonstration of the password application is depicted below.   


import base64
user = "geeksforgeeks"
passw = "i_lv_coding"
# Converting password to base64 encoding
passw_encoded = base64.b64encode(passw.encode('utf-8')).decode('utf-8')
user_login = "geeksforgeeks"
# Wrongly entered password
pass_wrong = "geeksforgeeks"
print("Password entered:", pass_wrong)
if pass_wrong == base64.b64decode(passw_encoded).decode('utf-8'):
    print("You are logged in!")
    print("Wrong Password!")
# Correctly entered password
pass_right = "i_lv_coding"
print("Password entered:", pass_right)
if pass_right == base64.b64decode(passw_encoded).decode('utf-8'):
    print("You are logged in!")
    print("Wrong Password!")


Password entered : geeksforgeeks
Wrong Password!!
Password entered : i_lv_coding
You are logged in!!

Working of the Python Decode() Method?

The following flowchart shows the working of Python decoding:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 12 Sep, 2023
Like Article
Save Article
Similar Reads
Related Tutorials