What does encode and decode mean python?

Python String encode()

Python string encode() function is used to encode the string using the provided encoding. This function returns the bytes object. If we don’t provide encoding, “utf-8” encoding is used as default.

Python Bytes decode()

Python bytes decode() function is used to convert bytes to string object. Both these functions allow us to specify the error handling scheme to use for encoding/decoding errors. The default is ‘strict’ meaning that encoding errors raise a UnicodeEncodeError. Some other possible values are ‘ignore’, ‘replace’ and ‘xmlcharrefreplace’. Let’s look at a simple example of python string encode() decode() functions.

str_original = 'Hello'

bytes_encoded = str_original.encode(encoding='utf-8')
print(type(bytes_encoded))

str_decoded = bytes_encoded.decode()
print(type(str_decoded))

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)

Output:

<class 'bytes'>
<class 'str'>
Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True

Above example doesn’t clearly demonstrate the use of encoding. Let’s look at another example where we will get inputs from the user and then encode it. We will have some special characters in the input string entered by the user.

str_original = input('Please enter string data:\n')

bytes_encoded = str_original.encode()

str_decoded = bytes_encoded.decode()

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)

Output:

What does encode and decode mean python?

Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: str.encode() API Doc, bytes.decode() API Doc

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up

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

    What does decode () do in Python?

    Python bytes decode() function is used to convert bytes to string object. Both these functions allow us to specify the error handling scheme to use for encoding/decoding errors. The default is 'strict' meaning that encoding errors raise a UnicodeEncodeError.

    What does encode () do in Python?

    The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.

    What is meant by encode and decode?

    In computers, encoding is the process of putting a sequence of characters (letters, numbers, punctuation, and certain symbols) into a specialized format for efficient transmission or storage. Decoding is the opposite process -- the conversion of an encoded format back into the original sequence of characters.

    What is decode (' utf

    Decoding UTF-8 Strings in Python To decode a string encoded in UTF-8 format, we can use the decode() method specified on strings. This method accepts two arguments, encoding and error . encoding accepts the encoding of the string to be decoded, and error decides how to handle errors that arise during decoding.