lev is an extremely fast Python library for comparing strings, written in Rust. 🦀 It computes the Levenshtein distance and a similarity ratio based on the longest common subsequence.
uv add lev-rsor with pip:
pip install lev-rsWheels are available for Python 3.10 through 3.15, including free-threaded Python (3.13t+), on Linux, macOS, and Windows.
import lev
lev.distance("kitten", "sitting") # 3
lev.distance("résumé", "resume") # 2
lev.distance("日本語", "日本") # 1
lev.ratio("kitten", "sitting") # 0.615...
lev.ratio("abc", "xyz") # 0.0
lev.ratio("", "") # 1.0lev.ratio returns the same values as rapidfuzz.fuzz.ratio(s1, s2) / 100 and Levenshtein.ratio.
See the API Reference for details.
lev is also published as the lev-rs crate:
cargo add lev-rsassert_eq!(lev::distance("kitten", "sitting"), 3);
assert_eq!(lev::ratio("abc", "xyz"), 0.0);The crate requires Rust 1.91 or newer. See docs.rs for details.
lev is compared against the fastest Levenshtein libraries for Python:
rapidfuzz,
polyleven,
editdistance, and
edlib.
Each plot shows the total time for 1,000 calls (lower is better).
Plots were generated by GitHub Actions on Ubuntu 24.04 with Python 3.13. Reproduce them locally with
uv run scripts/benchmark.py.
A short customer support message with a few natural typos.
Reproduce benchmarks locally using the provided scripts:
# Compare against other libraries:
uv run scripts/benchmark.py
# Benchmark scaling across string lengths:
uv run scripts/benchmark_by_length.py
# Benchmark speedup across released versions:
uv run scripts/benchmark_by_version.pyOpen an issue or email github@markusbilz.com.