Sitemap
Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Member-only story

Learn base64 Encoding and SHA-256/SHAKE-256 Hashing in Python

4 min readMar 8, 2024

--

Press enter or click to view image in full size
Image by geralt on Pixaby

String encoding and hashing are two distinct operations that serve different purposes in computing and data processing. Encoding is a reversible process that transforms a string into a sequence of bytes, whereas hashing is an irreversible process that produces a fixed-length string of characters, which usually appears as a seemingly random sequence of characters.

In this post, we will introduce the basic concepts of encoding and hashing in Python with simple but practical examples, especially using base64 for encoding and SHA-256/SHAKE-256 for hashing. Hopefully, you will then have a better understanding of how encoding and hashing work in Python.

Unicode and UTF-8

In Python 3, a string by default is a Unicode string, and we need to use b'' to explicitly mark a string as a binary string.

my_string = "Lynn"
my_binary = b"Lynn"

Alternatively, we can call the .encode() method of a string to encode the string using the specified encoding format, which is UTF-8 by default. UTF-8 is the most commonly used encoding format for Unicode characters.

--

--

Lynn G. Kwong
Lynn G. Kwong

Written by Lynn G. Kwong

I’m a Software Developer (https://lynn-kwong.medium.com/) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.

Python in Plain English
Python in Plain English

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.