How do you decode a value in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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

    str = "geeksforgeeks"

    str_enc = str.encode(encodeing='utf8'

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

    print (str_enc )

    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

    user = "geeksforgeeks"

    passw = "i_lv_coding"

    passw = passw.encode('base64', 'strict'

    user_login = "geeksforgeeks"

    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')

    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!!
    



    Description

    Python string method decode() decodes the string using the codec registered for encoding. It defaults to the default string encoding.

    Syntax

    Str.decode(encoding='UTF-8',errors='strict')
    

    Parameters

    • encoding − This is the encodings to be used. For a list of all encoding schemes please visit: Standard Encodings.

    • errors − This may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' and any other name registered via codecs.register_error().

    Return Value

    Decoded string.

    Example

    #!/usr/bin/python
    
    Str = "this is string example....wow!!!";
    Str = Str.encode('base64','strict');
    
    print "Encoded String: " + Str
    print "Decoded String: " + Str.decode('base64','strict')
    

    Result

    Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=
    Decoded String: this is string example....wow!!!
    

    python_strings.htm

    How do you decode data in Python?

    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.

    What does decoding mean in Python?

    Practical Data Science using Python To represent a unicode string as a string of bytes is known as encoding. To convert a string of bytes to a unicode string is known as decoding.

    What is the purpose of encode () and decode () method in Python?

    Since encode() converts a string to bytes, decode() simply does the reverse. This shows that decode() converts bytes to a Python string. Similar to those of encode() , the decoding parameter decides the type of encoding from which the byte sequence is decoded.

    How do you encode and decode a message in Python?

    Steps to develop message encode-decode project.
    Import Libraries. from tkinter import * ... .
    Initialize Window. root = Tk() ... .
    Define variables. Text = StringVar() ... .
    Function to encode. def Encode(key,message): ... .
    Function to decode. def Decode(key,message): ... .
    Function to set mode. def Mode(): ... .
    Function to exit window. ... .
    Function to reset window..