DEV Community

≀Paulo Portela
≀Paulo Portela

Posted on • Edited on

Working with Bytes in Python

In this post, we'll explore how to use bytes in Python.

Bytes are sequences of 8-bit integers, commonly used to represent binary data or text encoded in a specific character encoding. In Python, bytes can be created using the bytes or bytearray constructors, or by prefixing a string literal with the letter b:

# Using the bytes constructor
data = bytes([72, 101, 108, 108, 111])
print(data)

# Output: b'Hello'
Enter fullscreen mode Exit fullscreen mode
# Using the bytearray constructor
data = bytearray([72, 101, 108, 108, 111])
print(data)

# Output: bytearray(b'Hello')
Enter fullscreen mode Exit fullscreen mode
# Using a bytes literal
data = b"Hello"
print(data)

# Output: b'Hello'
Enter fullscreen mode Exit fullscreen mode

Bytes can be encoded and decoded using various character encodings, such as UTF-8 or ASCII. The encode() method can be used to encode a string into bytes using a specified encoding, and the decode() method can be used to decode bytes into a string using a specified encoding:

# Encoding a string into bytes
text = "Hello, world!"
data = text.encode(encoding="utf-8")
print(data)

# Output: b'Hello, world!'
Enter fullscreen mode Exit fullscreen mode
# Decoding bytes into a string
data = b"Hello, world!"
text = data.decode(encoding="utf-8")
print(text)

# Output: Hello, world!
Enter fullscreen mode Exit fullscreen mode

Python also provides the base64 module for encoding and decoding binary data using Base64. The b64encode() function can be used to encode binary data into a Base64-encoded string, and the b64decode() function can be used to decode a Base64-encoded string into binary data:

import base64

# Encoding binary data into a Base64-encoded string
data = b"Hello, world!"
encoded_data = base64.b64encode(s=data)
print(encoded_data)

# Output: b'SGVsbG8sIHdvcmxkIQ=='
Enter fullscreen mode Exit fullscreen mode
# Decoding a Base64-encoded string into binary data
encoded_data = b"SGVsbG8sIHdvcmxkIQ=="
data = base64.b64decode(s=encoded_data)
print(data)

# Output: b'Hello, world!'
Enter fullscreen mode Exit fullscreen mode

To remove the b character from the output of a bytes object, you can decode it into a string using the decode() method:

data = b"Hello, world!"
text = data.decode(encoding="utf-8")
print(text)

# Output: Hello, world!
Enter fullscreen mode Exit fullscreen mode

In conclusion, bytes are a powerful tool for representing and manipulating binary data in Python. With the ability to encode and decode bytes using various character encodings, as well as encode and decode binary data using Base64, you'll be able to work with binary data in a wide variety of applications.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more