How to Compress a String to Gzip Bytes in Python

Compress a string into gzip-compressed bytes entirely in memory using the standard library gzip module.

Easy Python 3.9+ Aug 9, 2026 Files & data 14 views 0 copies

Python code

12 lines
Python 3.9+
import gzip

def compress_to_gzip_bytes(data: str, encoding: str = "utf-8") -> bytes:
    """Compress a string to gzip-compressed bytes in memory."""
    return gzip.compress(data.encode(encoding))

if __name__ == "__main__":
    original = "Hello, world! " * 10
    compressed = compress_to_gzip_bytes(original)
    print(f"Original size: {len(original.encode())} bytes")
    print(f"Compressed size: {len(compressed)} bytes")
    print(f"Compressed bytes (first 20): {compressed[:20]}")

Output

stdout
Original size: 130 bytes
Compressed size: 47 bytes
Compressed bytes (first 20): b'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xed\xc1\x01\x01\x00\x00\x00\x80\x90'

How it works

The gzip.compress function takes a bytes-like object and returns gzip-compressed bytes, avoiding the overhead of writing to a file. Encoding the string with data.encode(encoding) converts it to bytes before compression. This is ideal for in-memory operations like sending compressed payloads over HTTP or storing in caches. The compression level defaults to 9, but you can adjust it with the compresslevel parameter for speed vs. size trade-offs.

Common mistakes

  • Forgetting to encode the string to bytes before calling gzip.compress.
  • Assuming gzip.compress works directly with str objects.
  • Ignoring the compresslevel parameter when speed matters more than size.
  • Confusing gzip.compress (bytes in, bytes out) with gzip.open (file operations).

Variations

  1. Use gzip.compress(data.encode(), compresslevel=6) to balance speed and compression ratio.
  2. Use zlib.compress for raw deflate without the gzip header if you need smaller size.

Real-world use cases

  • Sending compressed JSON payloads to a message queue to reduce network bandwidth.
  • Storing compressed logs or text blobs in Redis or a cache to save memory.
  • Compressing API responses before writing to a cloud storage object.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.