If you have data as Base64 String that you want to decode using Python, you can make use of the base64 module.
Let's take a look at it by an example.
Example:import base64
base64_encoded_string = 'SGVsbG8gUHl0aG9uIQ=='
base64_decoded_bytes = base64.b64decode(base64_encoded_string)
base64_decoded_string = base64_decoded_bytes.decode('utf-8')
print(base64_decoded_string)
Output:
We have achieved this in two steps by using two methods from the base64 modules:
1. base64.b64decode(s, altchars=None, validate=False)
This method decodes the Base64-encoded bytes or ASCII string and returns the decoded data as bytes.
Parameters:s: The Base64-encoded string or bytes to decode.
altchars (optional): An alternative set of characters used for the encoded string.
validate (optional): If True, perform additional validation checks on the input. Defaults to False.
Returns: The decoded data as bytes.
2. bytes.decode(encoding='utf-8', errors='strict')
This method decodes the bytes object using the specified encoding and returns a string.
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
More Posts:
- Native getClass() method from java.lang.Object Class Explained with examples. - Java
- How to Delete a Sheet in Excel on Mac - Microsoft
- [fix] Spring Boot Data JPA - No identifier specified for entity - Java
- How to Indent/Prettify HTML File in Notepad++ - NotepadPlusPlus
- How to turn on channel notifications for Microsoft Teams - Teams
- How to check about details of Notepad++ text editor - NotepadPlusPlus
- Fix: Mac Screenshot opens in Mail Application - MacOS
- TextEdit Clear Formatting when Pasting Text - MacOS