Sling Academy
Home/Python/How to base64 encode/decode a string in Python

How to base64 encode/decode a string in Python

Last updated: June 01, 2023

This succinct, practical article shows you how to base64 encode/decode a string in Python.

Base64 encode a string

Base64 encoding is a process of converting a string into a format that uses a set of 64 characters to represent binary data, allowing safe transmission and storage of data that may contain special or non-printable characters.

In Python, you can use the b64encode() function from the base64 module to get the job done. Just follow the steps listed below:

  1. Import the base64 module.
  2. Encode the string to bytes using the appropriate encoding (e.g., UTF-8).
  3. Pass the encoded bytes to the base64.b64encode() function.
  4. Decode the resulting bytes object to a string using the appropriate decoding (e.g., UTF-8).

Code example:

import base64

string = "Welcome to Sling Academy!"

# Encode using base64.b64encode() function
encoded_string = base64.b64encode(string.encode("utf-8")).decode("utf-8")

print(encoded_string)

Output:

V2VsY29tZSB0byBTbGluZyBBY2FkZW15IQ==

Decode a base64 string to a human-readable string

Base64 decoding is the reverse process of base64 encoding. It involves converting a base64-encoded string back to its original form and recovering the original data from the encoded representation.

The way to decode a base64 string in Python is to use the b64decode() function from the base64 module. Below are the steps:

  1. Import the base64 module.
  2. Use the base64.b64decode() function to decode the string.
  3. Decode the result using the appropriate character encoding (mostly UTF-8).

Code example:

import base64

encoded_string = "V2VsY29tZSB0byBTbGluZyBBY2FkZW15IQ=="
decoded_bytes = base64.b64decode(encoded_string)
decoded_string = decoded_bytes.decode("utf-8")

print(decoded_string)

Output:

Welcome to Sling Academy!

Next Article: Python: 3 Ways to Add Leading Zeros to a String

Previous Article: 2 ways to get the size of a string in Python (in bytes)

Series: Working with Strings in Python

Python

You May Also Like

  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types
  • Python: Typing a function with *args and **kwargs