Skip to main content

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

Metadata

Release files for py-lzstring 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-lzstring 0.1.1
File Size Uploaded
py_lzstring-0.1.1.tar.gz 25.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-lzstring 0.1.1
File Interpreter ABI Platform
py_lzstring-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 53.8 kB

Release files / py_lzstring-0.1.1.tar.gz

Download URL py_lzstring-0.1.1.tar.gz
Size 25.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3524381e21f4096ee2637383d4f4b66e518a27004a68996787e986df5a1f377c
BLAKE2b-256 checksum
How to use checksums
cd62c600cdcab36d4e62dfe6fa5a59c0fb417eb580b32935ba882c3db3b2dd1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / py_lzstring-0.1.1-py3-none-any.whl

Download URL py_lzstring-0.1.1-py3-none-any.whl
Size 28.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c484d585738d6025b879b878cf505a0c3fc2bfe55b51f7c3d9d0f667d18041e4
BLAKE2b-256 checksum
How to use checksums
5dc8a4a33b76b6298b5621148eb220197db152685af62236caf7c18683f7758f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page