Python Levenshtein: edit distance and string similarity in Python

The Levenshtein distance between two strings is the minimum number of single-character insertions, deletions and substitutions needed to turn one into the other. kitten → sitting is 3: substitute k→s, substitute e→i, insert g. The Levenshtein package computes this (and a few related similarity measures) in C++, so it is much faster than a pure-Python implementation.
Installation
Section titled “Installation”pip install LevenshteinVersion 0.27.5 (the current release when this was revised) requires Python 3.11 or newer. The package is licensed GPL-2.0-or-later, which matters if you ship it inside proprietary software; RapidFuzz, which Levenshtein is built on, offers the same metrics under the MIT licence.
Distance
Section titled “Distance”import Levenshtein
print(Levenshtein.distance("kitten", "sitting")) # 3distance also takes a score_cutoff. If the real distance is larger than the cutoff, it returns score_cutoff + 1 and can stop early, which helps when you only care about “close enough” matches:
print(Levenshtein.distance("kitten", "sitting", score_cutoff=2)) # 3, i.e. "more than 2"Ratio and Jaro-Winkler
Section titled “Ratio and Jaro-Winkler”Raw distances are hard to compare across strings of different lengths, so the package also gives normalised scores between 0 and 1:
import Levenshtein
a, b = "Python", "Pythin"
print(Levenshtein.distance(a, b)) # 1print(Levenshtein.ratio(a, b)) # 0.8333333333333334print(Levenshtein.jaro(a, b)) # 0.888888888888889print(Levenshtein.jaro_winkler(a, b)) # 0.9333333333333333ratiois1 - indel_distance / (len(a) + len(b)), where the indel distance only allows insertions and deletions (a substitution counts as two edits). Here that is 1 − 2/12.jarocounts matching characters within a sliding window and penalises transpositions.jaro_winklerboosts the Jaro score when the strings share a prefix (up to 4 characters).PythonandPythinsharePyth, so the score rises from 0.889 to 0.933. It works well for short strings such as names.
Seeing the edits
Section titled “Seeing the edits”editops and opcodes tell you which edits make up the distance, which is useful for highlighting differences:
from Levenshtein import opcodes
for op in opcodes("kitten", "sitting"): print(op)('replace', 0, 1, 0, 1)('equal', 1, 4, 1, 4)('replace', 4, 5, 4, 5)('equal', 5, 6, 5, 6)('insert', 6, 6, 6, 7)Each tuple is (tag, a_start, a_end, b_start, b_end), the same format as difflib.SequenceMatcher.get_opcodes().
A small spelling suggester
Section titled “A small spelling suggester”from Levenshtein import distance
words = ["apple", "apply", "ample", "maple", "angle"]typo = "appel"
ranked = sorted(words, key=lambda w: distance(typo, w))print(ranked[:3]) # ['apple', 'apply', 'ample']This compares the typo against every word, which is fine for a few thousand entries. For large dictionaries or fuzzy matching across big datasets, use rapidfuzz.process.extract, which batches the comparisons and supports cutoffs, or an index such as a BK-tree.
Gotchas
Section titled “Gotchas”- Comparisons are case- and whitespace-sensitive. Normalise first (
.lower(),.strip(), Unicode normalisation) if that is not what you want. - Edit distance measures spelling, not meaning. “car” and “automobile” are far apart.
Levenshtein.ratioreturns 0–1. Thefuzz.ratiointhefuzz/rapidfuzzuses the same indel-based measure but scales it to 0–100, so don’t mix thresholds between the two.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)