python-levenshtein

0.27.3 · active · verified Mon Apr 06

python-levenshtein is a Python C extension module providing highly optimized functions for computing string edit distances (like Levenshtein distance), similarity ratios, and related metrics. While the package itself has been renamed to `levenshtein` and is actively maintained under that name by the RapidFuzz team, the `python-levenshtein` PyPI package (version 0.27.3) continues to be updated as a compatibility wrapper. It maintains a positive release cadence.

Warnings

Install

Imports

Quickstart

Calculate the Levenshtein distance (minimum number of single-character edits) and a similarity ratio between two strings.

import Levenshtein

str1 = "kitten"
str2 = "sitting"

# Calculate Levenshtein distance
distance = Levenshtein.distance(str1, str2)
print(f"Levenshtein distance between '{str1}' and '{str2}': {distance}")

# Calculate similarity ratio
ratio = Levenshtein.ratio(str1, str2)
print(f"Similarity ratio between '{str1}' and '{str2}': {ratio:.2f}")

# Example with different strings
str3 = "hello"
str4 = "hallo"
distance2 = Levenshtein.distance(str3, str4)
ratio2 = Levenshtein.ratio(str3, str4)
print(f"\nLevenshtein distance between '{str3}' and '{str4}': {distance2}")
print(f"Similarity ratio between '{str3}' and '{str4}': {ratio2:.2f}")

view raw JSON →