base64.
How It Works
base64.b32encode() encodes raw bytes using a case-insensitive-friendly alphabet of capital letters A-Z and digits 2-7. Base32 usually produces longer output than Base64 and may include = padding.
Step-by-Step Example
import base64
sample_bytes = b'backup-2026'
encoded_bytes = base64.b32encode(sample_bytes)
print(encoded_bytes.decode('ascii'))
print(base64.b32decode(encoded_bytes) == sample_bytes)
Understanding the Example
- The expression "b'backup-2026'" supplies the value stored as 'sample_bytes'. This value is then used or checked by the later statements.
- The expression 'base64.b32encode(sample_bytes)' supplies the value stored as 'encoded_bytes'. This value is then used or checked by the later statements.
A successful run of this example prints the following output:
MJQWG23VOAWTEMBSGY======
True
Where This Fits in a Real Program
This transformation changes how bytes are represented for transport. It does not hide the original information or prove authenticity, so avoid using it as a substitute for encryption or a digital signature. When handling text, encode the characters to bytes first and decode restored bytes using the matching character encoding.
Practice: Try It Yourself
Display Base32 and Base64 lengths for the same short binary message.
For this exercise, predict the result before running the changed code. Then explain the difference between the original and modified cases using the actual values, not just whether the program completed.
Further Reading
Read the official Python documentation for complete parameters, return types and exceptions.
Common Beginner Mistakes
- Expecting Base32 to use every digit or to produce a shorter representation than Base64.
- Do not mistake successful decoding or parsing for trust in the contents.
Key Takeaways
- base64.
- base64.b32encode() encodes raw bytes using a case-insensitive-friendly alphabet of capital letters A-Z and digits 2-7.
- Apply the demonstrated operation to the practice input and verify its result before depending on it in a larger program.