Skip to main content

LZ-based string compression - Python port of lz-string@1.5.0 (JavaScript)

Project description

py-lzstring

Python port of lz-string - byte-for-byte compatible with lz-string@1.5.0.

Installation

pip install py-lzstring

No runtime dependencies. Requires Python 3.10+.

Quickstart

import lzstring

# Compress to base64 (safe for HTTP, JSON, localStorage);
compressed = lzstring.compress_to_base64("Hello, World!")
print(compressed)       # 'BIUwNmD2A0AEDqkBOYAmBCIA';

original = lzstring.decompress_from_base64(compressed)
print(original)     # 'Hello, World!';

Encodings

Function pair bits/char Use case
compress / decompress 16 In-memory; smallest output
compress_to_utf16 / decompress_from_utf16 15 localStorage (all browsers)
compress_to_base64 / decompress_from_base64 6 HTTP, JSON, data-URLs
compress_to_encoded_uri_component / decompress_from_encoded_uri_component 6 URL query strings
compress_to_uint8array / decompress_from_uint8array Binary I/O, bytes output

Interoperability with JavaScript

All encodings are bit-exact with lz-string@1.5.0:

// JavaScript
const LZString = require("lz-string");
const compressed = LZString.compressToBase64("Hello, World!");
// "BIUwNmD2A0AEDqkBOYAmBCIA"
# Python
import lzstring
lzstring.decompress_from_base64("BIUwNmD2A0AEDqkBOYAmBCIA")
# "Hello, World!"

API reference

All compress functions accept str | None and return str (or bytes for the Uint8Array variant). Passing None mirrors the JavaScript null-handling and returns "".

All decompress functions accept str | None (or bytes | None for Uint8Array) and return str | None. An empty string returns None (like JS null), indicating invalid input.

import lzstring
 
# Raw (most compact, arbitrary Unicode output)
lzstring.compress("...")
lzstring.decompress("...")
 
# UTF-16 (printable characters only, safe for all localStorage implementations)
lzstring.compress_to_utf16("...")
lzstring.decompress_from_utf16("...")
 
# Base64 (standard alphabet with = padding)
lzstring.compress_to_base64("...")
lzstring.decompress_from_base64("...")
 
# URI component (URL-safe, no padding)
lzstring.compress_to_encoded_uri_component("...")
lzstring.decompress_from_encoded_uri_component("...")
 
# Uint8Array (returns / accepts bytes)
lzstring.compress_to_uint8array("...")      # → bytes
lzstring.decompress_from_uint8array(b"...") # → str | None

License

MIT License

Copyright 2026 Frank Hoffmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Thanks

Many thanks to all the developers of the libraries used and to the community for creating so many incredibly useful tools.

AI Usage

I used the AI assistant Anthropic Claude AI - Sonnet 4.6 to create this tool.

As a computer scientist, I have reviewed and approved every single line of code, and I understand the tool’s internal processes and how it works. I didn’t just copy and paste the code from the AI. Instead, I wrote it by hand, line by line, making changes whenever I deemed it necessary.

Nevertheless, there may still be errors or poor design choices. Everyone is free to examine, modify, improve, fork the code or call it AI slop :D

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_lzstring-0.1.0.tar.gz (25.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_lzstring-0.1.0-py3-none-any.whl (27.4 kB view details)

Uploaded Python 3

File details

Details for the file py_lzstring-0.1.0.tar.gz.

File metadata

  • Download URL: py_lzstring-0.1.0.tar.gz
  • Upload date:
  • Size: 25.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for py_lzstring-0.1.0.tar.gz
Algorithm Hash digest
SHA256 853200bfaafeb53e3471b5cec5540f87301105eb4548a68ac086abb40f2b2f16
MD5 265023f5df438e475b4550bbc7a4f06e
BLAKE2b-256 aa7c5d708fdfd49a74e3c3a0770c9b027fbf420ca201062bcf17cf7e6e62c978

See more details on using hashes here.

File details

Details for the file py_lzstring-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: py_lzstring-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 27.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for py_lzstring-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bfe35e106506c8d137c6c0b4624f3c9e1c2d484f095aac97e2a06c8a0c3f8f37
MD5 e8c666717cdc4cf6f6edd831afc4ef1d
BLAKE2b-256 cfdbe8695c4482381be211b52335cba1dde3f1521f1078e69a3f61861fac0025

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page