Python String decode() method
The string.decode() method decodes the string using the codec registered for encoding. Default encoding is the current default string encoding. It may raise errors to set a different error handling scheme.
Syntax
1 2 3 |
decode([encoding[, errors]]) |
Parameter
Encoding: This parameter represents a String specifying the encoding to use. The default value is UTF-8
Errors: This parameter is given to set a different error handling scheme. The default for errors is ‘strict’, meaning that encoding errors raise a UnicodeError.
Return
This method returns the decoded string.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 |
# Python String program explaining # the decode() method # initializing the string Str = "This is my Python String example"; # encoding the specified string Str = Str.encode('base64','strict'); # printing the encoded string print ("Encoded String: " + Str) # decoding the encoded string print "Decoded String: " + Str.decode('base64','strict') |
Output
1 2 3 4 |
Encoded String: VGhpcyBpcyBteSBQeXRob24gU3RyaW5nIGV4YW1wbGU= Decoded String: This is my Python String example |
Example 2
1 2 3 4 5 6 7 8 9 10 |
# Python String program explaining # the decode() method # initializing the string txt = "My name is Reema" # decoding the string x = txt.decode() # printing the decoded string print(x) |
Output
1 2 3 |
My name is Reema |