Python Base64 Decoding Example

In this source code example, we show you how to decode any Base64 encoded data back to binary data.

Python’s base64 module provides functions to perform Base64 encoding and decoding as described in RFC 3548.

Python Base64 Decoding Example

import base64

encodedStr = "aGVsbG8gd29ybGQxMjMhPyQ="

# Standard Base64 Decoding
decodedBytes = base64.b64decode(encodedStr)
decodedStr = str(decodedBytes, "utf-8")
print(decodedStr)

# Url Safe Base64 Decoding
decodedBytes = base64.urlsafe_b64decode(encodedStr)
decodedStr = str(decodedBytes, "utf-8")
print(decodedStr)
Output:
hello world123!?$
hello world123!?$
Let's understand the above source code step by step.

Python Base64 Decoding:

encodedStr = "aGVsbG8gd29ybGQxMjMhPyQ="

# Standard Base64 Decoding
decodedBytes = base64.b64decode(encodedStr)
decodedStr = str(decodedBytes, "utf-8")
print(decodedStr)

Python Base64 URL safe Decoding:

# Url Safe Base64 Decoding
decodedBytes = base64.urlsafe_b64decode(encodedStr)
decodedStr = str(decodedBytes, "utf-8")
print(decodedStr)
 

Comments