Levenshtein String Distance

0.27.3 · active · verified Sun Mar 29

The `levenshtein` Python C extension module provides highly optimized functions for fast computation of Levenshtein (edit) distance, string similarity, and other related metrics. It is currently at version 0.27.3 and maintains an active release cadence with regular updates to support newer Python versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to calculate the Levenshtein distance, the normalized similarity ratio, and retrieve the sequence of edit operations between two strings using the `levenshtein` library.

import Levenshtein

string1 = "kitten"
string2 = "sitting"

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

# Calculate Levenshtein ratio (normalized similarity)
ratio = Levenshtein.ratio(string1, string2)
print(f"Levenshtein ratio between '{string1}' and '{string2}': {ratio:.2f}")

# Get edit operations
edit_ops = Levenshtein.editops(string1, string2)
print(f"Edit operations: {edit_ops}")

view raw JSON →