thefuzz (Fuzzy String Matching)

0.22.1 · active · verified Thu Apr 09

thefuzz is a Python library for fuzzy string matching, based on Levenshtein distance. It provides a simple API for comparing strings and extracting best matches from collections. The current version is 0.22.1, and it maintains an active development pace with periodic releases.

Warnings

Install

Imports

Quickstart

This example demonstrates basic ratio calculation between two strings and how to find the best (or top N) matches for a query string within a list of choices using `fuzz` and `process` modules.

from thefuzz import fuzz
from thefuzz import process

# Basic string comparison
score = fuzz.ratio("this is a test", "this is a test!")
print(f"Ratio score: {score}")

# Find the best match in a list
choices = ["apple pie", "grapefruit", "apple tree"]
query = "apple"
best_match, best_score = process.extractOne(query, choices)
print(f"Best match for '{query}': '{best_match}' with score {best_score}")

# Get top N matches
top_matches = process.extract(query, choices, limit=2)
print(f"Top matches for '{query}': {top_matches}")

view raw JSON →