Python Strings decode() method

Last Updated : 11 May, 2026

decode() method is used to convert encoded text back into its original string format. It works as the opposite of encode() method, which converts a string into a specific encoding format. For example, let's try to encode and decode a simple text message:

Python
s = "Geeks for Geeks"

e = s.encode('utf-8')
print("Encoded text:", e)
print(type(e))

d = e.decode('utf-8')
print(type(d))
print("Decoded text:", d)

Output
Encoded text: b'Geeks for Geeks'
<class 'bytes'>
<class 'str'>
Decoded text: Geeks for Geeks

Explanation:

  • s.encode('ut-8') encodes the text into bytes format.
  • e.decode('utf-8') decodes the encoded text back to normal unicode format.

Syntax

encoded_string.decode(encoding, errors)

Parameters:

1. encoding: The encoding format used for decoding (e.g., utf-8, ascii).

2. errors (Optional): Specifies how to handle errors during decoding:

  • strict (default): Raises an error for invalid characters.
  • ignore: Ignores errors and proceeds.
  • replace: Replaces invalid characters with a placeholder.

Return Type: Returns the original string after decoding.

Working of decode() Method

The following flowchart shows the working of Python decoding:

working of decode() in python

Examples

Example 1: Basic Encoding and Decoding

Encoding converts a string into bytes and decode() brings it back to its original form.

Python
t = "Hello, Python!"

e_t = t.encode('utf-8')
print("Encoded:", e_t)

d_t = e_t.decode('utf-8')
print("Decoded:", d_t)

Output
Encoded: b'Hello, Python!'
Decoded: Hello, Python!

Explanation:

  • encode('utf-8') converts the string into bytes format.
  • decode('utf-8') restores the original string.

Example 2: Handling Decoding Errors

Sometimes, decoding fails due to incompatible characters. Let's see how different error-handling modes work:

Python
s = "Café"

# Encoding the text in ASCII
enc = s.encode('ascii', errors='replace')

# Decoding with strict mode (raises error)
try:
    print(enc.decode('ascii', errors='strict'))
except UnicodeDecodeError as e:
    print("Decoding Error:", e)

# Decoding with ignore mode (ignores errors)
print("Ignored Decoding:", enc.decode('ascii', errors='ignore'))

# Decoding with replace mode (replaces errors)
print("Replaced Decoding:", enc.decode('ascii', errors='replace'))

Output
Caf?
Ignored Decoding: Caf?
Replaced Decoding: Caf?

Explanation:

  • strict mode raises an error when an unsupported character (é) is found.
  • ignore mode removes unsupported characters (é).
  • replace mode replaces unsupported characters with a placeholder (?).

Example 3: Password Encoding & Decoding

Encoding (like Base64) is not secure and should not be used for storing passwords. Here’s a simple demonstration.

Python
import base64

# User credentials
user = "user1"
passw = "secure@123"

# Encoding password
enc_pass = base64.b64encode(passw.encode('utf-8')).decode('utf-8')
print("Encoded Password:", enc_pass)

# Decoding password for verification
dec_pass = base64.b64decode(enc_pass).decode('utf-8')
print("Decoded Password:", dec_pass)

# Login verification
e_pass = "secure@123"
if e_pass == dec_pass:
    print("Login Successful!")
else:
    print("Wrong Password!")

Output
Encoded Password: c2VjdXJlQDEyMw==
Decoded Password: secure@123
Login Successful!

Explanation:

  • Base64 encoding only converts data into a different format and is easily reversible, so it does not provide security.
  • It is later decoded to verify the login.

Note: In real applications, passwords should be stored using hashing (e.g., Django’s built-in password hashing or algorithms like bcrypt), not encoding

Use Cases

  • Helps retrieve the original text from an encoded format.
  • Essential for handling different character encodings like UTF-8, ASCII, etc.
  • Useful in data transmission, security applications and text processing.
Comment