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.1.tar.gz (25.8 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.1-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_lzstring-0.1.1.tar.gz
  • Upload date:
  • Size: 25.8 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.1.tar.gz
Algorithm Hash digest
SHA256 3524381e21f4096ee2637383d4f4b66e518a27004a68996787e986df5a1f377c
MD5 c54122044573d7ad18954a616bfb407a
BLAKE2b-256 cd62c600cdcab36d4e62dfe6fa5a59c0fb417eb580b32935ba882c3db3b2dd1f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_lzstring-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c484d585738d6025b879b878cf505a0c3fc2bfe55b51f7c3d9d0f667d18041e4
MD5 05d2c4a8ba8e5df29032151c22a36448
BLAKE2b-256 5dc8a4a33b76b6298b5621148eb220197db152685af62236caf7c18683f7758f

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