Base64 String Encoding in Python

  • Post category:String

In this article, you will learn string encoding in Python. Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. In Python, you can use the base64 module to encode and decode a string.

Step 1: Import base64 library

import base64

Step 2: Create a string

str = "CodeAllow"
print("str = ", str)
print("str type = ", type(str))

Step 3: Base64 string encoding

str = base64.b64encode(str.encode())
print("str = ", str)
print("str type = ", type(str))


str =  CodeAllow
str type =  <class 'str'>
str =  b'Q29kZUFsbG93'
str type =  <class 'bytes'>

Free resources to learn advanced skills: AiHints and CodeAllow