Explain Codes LogoExplain Codes Logo

Why do I need 'b' to encode a string with Base64?

python
base64-encoding
unicode-strings
bytes-literals
Anton ShumikhinbyAnton Shumikhin·Mar 13, 2025
TLDR

Before Base64 can encode your string, it needs to be a byte string (b'...') in Python. Here's the deal: you use the encode() method to change the string into bytes, and then presto! — base64.b64encode() is your friend.

import base64

You see, I'm a programmer, I have no life. So let's encode it!

encoded = base64.b64encode("My life".encode()) print(encoded) # Now, you too, can have no life in Base64!

Always remember, folks, **`.encode()`** gets you bytes and **`base64.b64encode()`** can't breathe without bytes.

## Understanding Behind the Scenes: Unicode and Byte Strings
In **Python 3**, strings are all about **Unicode characters**, not the raw 8-bit data Base64 encoding feasts upon. The **`b''`** notation bucks up a **bytes literal** for Base64 to tangle with. Just so you see what's kicking:

Original - Unicode String: `'Hello'`

Transformed - Byte String: `b'Hello'`

## Knowing Your Base64 Beasts: Varying Data Types
When using **`base64.b64encode`**, it's not all strings and bytes, sometimes you wrestle different beasts — like **image files** or **binary files**. These creatures are already in binary format, so just read and feed to `base64.b64encode`. No more encoding hoops to jump through!

```python
with open('maxresdefault.jpg', 'rb') as image_file:
    encoded_string = base64.b64encode(image_file.read())
    ## Who knew JPG could look so... spammy?

Base64 goes where text is expected but binary is needed. Common destinations include:

  • Email attachments: Base64 whips binary files into text characters fit for transport.
  • Data URLs: Baking small images into HTML/CSS using Base64 bakes also HTTP requests.

Ducking the Errors: Common Base64 Missteps

With great power of encoding, comes great responsibility to avoid these common blunders:

  1. Trying to feed Unicode strings to Base64 without converting to bytes.
  2. Encoding non-ASCII text? Mind you specify the correct encoding like utf-8.

Working with Unicorns: Unicode and Base64

To tame the unicorns (read: Unicode strings) into Base64 encoded form, you follow a two-step ritual:

  1. Convert the unicorn strays into a herd of bytes.
  2. Bless the herd with the Base64 magic.

Encoding - Unicorns to Base64

unicode_stray = 'こんにちは' bytes_herd = unicode_stray.encode('utf-8') base64_magic = base64.b64encode(bytes_herd)

Decoding - Base64 Magic back to Unicorns

decoded_herd = base64.b64decode(base64_magic) decoded_stray = decoded_herd.decode('utf-8') ## Unicorn to bytes to Base64. Base64 to bytes to unicorn. Circle of life!

Do remember to handle any sharp horns during the process!

Since We're Amongst Friends: Base64 Encoding Best Practices

Practices that'll guarantee you a spot in the Popular Programmers' Pinterest:

  • Tackle the fearsome UnicodeEncodeError when handling strings.
  • Spot errors early with .decode('ascii') while verifying encoded data.
  • Keep an eye on the pesky padding character "=" when dealing with leftovers or partial data.