decode() is a method specified in Strings in Python 2.
This method is used to convert from one encoding scheme, in which 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.
Syntax : decode(encoding, error)
Parameters :
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.
Code #1 : Code to decode the string
# Python code to demonstrate # decode() # initializing string str = "geeksforgeeks" # encoding string str_enc = str.encode(encodeing='utf8') # printing the encoded string print ("The encoded string in base64 format is : ",) print (str_enc ) # printing the original decoded string print ("The decoded string is : ",) print (str_enc.decode('utf8', 'strict')) |
Output:
The encoded string in base64 format is : Z2Vla3Nmb3JnZWVrcw== The decoded string is : geeksforgeeks
Application :
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 the information confidential.
A small demonstration of the password application is depicted below.
Code #2 : Code to demonstrate application of encode-decode
# Python code to demonstrate # application of encode-decode # input from user # user = input() # pass = input() user = "geeksforgeeks"passw = "i_lv_coding" # converting password to base64 encoding passw = passw.encode('base64', 'strict') # input from user # user_login = input() # pass_login = input() user_login = "geeksforgeeks" # wrongly entered password pass_wrong = "geeksforgeeks" print ("Password entered : " + pass_wrong ) if(pass_wrong == passw.decode('base64', 'strict')): print ("You are logged in !!") else : print ("Wrong Password !!") print( '\r') # correctly entered password pass_right = "i_lv_coding" print ("Password entered : " + pass_right ) if(pass_right == passw.decode('base64', 'strict')): print ("You are logged in !!") else : print ("Wrong Password !!") |
Output:
Password entered : geeksforgeeks Wrong Password!! Password entered : i_lv_coding You are logged in!!
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Message Encode-Decode using Tkinter
- numpy.defchararray.decode() in Python
- Python | Pandas Series.str.decode()
- codecs.decode() in Python
- How To Encode And Decode A Message using Python?
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python Strings encode() method
- Interesting facts about strings in Python | Set 1
- Python | Converting all strings in list to integers
- Python sorted() to check if two strings are anagram or not
- Generate two output strings depending upon occurrence of character in input string in Python
- Python Set | Pairs of complete strings in two sets
- Replacing strings with numbers in Python for Data Analysis
- Python | Remove last character in list of strings
- Python | Scramble strings in list
- Python | Program to accept the strings which contains all vowels
- Python | Filter a list based on the given list of strings
- Python | Extract K sized strings
- Python | Pandas Split strings into two List/Columns using str.split()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

