Last Updated : 01 Jul, 2024
Summarize
Comments
Improve
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)
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.
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 stringString = "geeksforgeeks"encoded_string = String.encode('utf-8')print('The encoded string in base64 format is :')print(encoded_string)decoded_string = encoded_string.decode('utf-8')print('The decoded string is :')print(decoded_string)
Output:
The encoded string in base64 format is :
b'geeksforgeeks'
The decoded string is :
geeksforgeeks
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 base64user = "geeksforgeeks"passw = "i_lv_coding"# Converting password to base64 encodingpassw_encoded = base64.b64encode(passw.encode('utf-8')).decode('utf-8')user_login = "geeksforgeeks"# Wrongly entered passwordpass_wrong = "geeksforgeeks"print("Password entered:", pass_wrong)if pass_wrong == base64.b64decode(passw_encoded).decode('utf-8'): print("You are logged in!")else: print("Wrong Password!")print()# Correctly entered passwordpass_right = "i_lv_coding"print("Password entered:", pass_right)if pass_right == base64.b64decode(passw_encoded).decode('utf-8'): print("You are logged in!")else: print("Wrong Password!")
Output:
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:
Decode()
Python Strings decode() method – FAQs
How to use decode
in Python 3?
In Python 3, the
decode
method is used to convert abytes
object into astr
(string) object by decoding it from a specific encoding.Example:
# Define a bytes object
bytes_obj = b'Hello, world!'# Decode bytes object to string using UTF-8 encoding
string_obj = bytes_obj.decode('utf-8')print(string_obj) # Output: Hello, world!
Here,
decode('utf-8')
converts the bytes object from UTF-8 encoding to a string.
What does .decode('utf-8')
do?
The
.decode('utf-8')
method converts abytes
object into astr
object using the UTF-8 encoding. UTF-8 is a variable-width character encoding used for text.Example:
# Define a bytes object with UTF-8 encoding
bytes_obj = b'\xe2\x9c\x94'# Decode bytes object to string
string_obj = bytes_obj.decode('utf-8')print(string_obj) # Output: ✓
What is string decoding?
String decoding is the process of converting encoded bytes back into a string. It interprets bytes according to a specified character encoding to produce a readable string.
Example:
# Define a bytes object
bytes_obj = b'Hello'# Decode bytes object to string
string_obj = bytes_obj.decode('utf-8')print(string_obj) # Output: Hello
What is an example of decode
?
Here’s a basic example of how to use the
decode
method with a bytes object:Example:
# Define a bytes object
bytes_data = b'Hello, Python!'# Decode bytes object to string using UTF-8 encoding
text = bytes_data.decode('utf-8')print(text) # Output: Hello, Python!
What is encode()
in Python?
The
encode()
method is used to convert a string into abytes
object using a specific encoding. This is the reverse ofdecode()
.Example:
# Define a string
text = 'Hello, world!'# Encode string to bytes using UTF-8 encoding
bytes_obj = text.encode('utf-8')print(bytes_obj) # Output: b'Hello, world!'
Additional Example of
encode()
:# Define a string
text = 'Hello, Python!'# Encode string to bytes
bytes_data = text.encode('utf-8')print(bytes_data) # Output: b'Hello, Python!'