Fuzzy String Matching in Python (difflib and RapidFuzz)

Fuzzy string matching in Python scores how similar two strings are, so “Philadelpia” can still match “Philadelphia”. The standard library’s difflib does it with no installs, and the RapidFuzz package does it much faster with smarter scorers:

from difflib import get_close_matches
get_close_matches("San Fransisco", ["San Francisco", "San Diego"])

from rapidfuzz import fuzz
fuzz.ratio("Apple Inc.", "Apple Incorporated")

All scores below come from real runs of Python 3.12.5 with RapidFuzz 3.14.6; install RapidFuzz with pip install rapidfuzz.

We’ll start with difflib, compare RapidFuzz’s four main scorers, find the best match in a list, group duplicate customer names, and see how much faster RapidFuzz is.

Fuzzy matching with difflib (built in)

You get a similarity from 0 to 1 with SequenceMatcher.ratio(), and get_close_matches() picks the closest items from a list, which is perfect for typo correction:

from difflib import SequenceMatcher, get_close_matches

print(round(SequenceMatcher(None, "Philadelphia", "Philadelpia").ratio(), 3))
print(round(SequenceMatcher(None, "Denver", "Dallas").ratio(), 3))

cities = ["San Francisco", "San Diego", "San Antonio", "Sacramento", "Santa Fe"]
for typo in ["San Fransisco", "Sacremento", "San Deigo", "Boston"]:
    print(f"{typo:<14} -> {get_close_matches(typo, cities, n=1, cutoff=0.6)}")

Output:

0.957
0.167
San Fransisco  -> ['San Francisco']
Sacremento     -> ['Sacramento']
San Deigo      -> ['San Diego']
Boston         -> []
Command Prompt output of Python difflib SequenceMatcher ratios and get_close_matches correcting misspelled US city names
Three typos find the right city; “Boston” isn’t close to any of them, so it returns an empty list.

You set how similar a match must be with cutoff. Raise it to avoid wrong matches, lower it to catch worse typos.

RapidFuzz scorers: ratio, partial_ratio, token_sort_ratio and WRatio

RapidFuzz scores from 0 to 100 and gives you scorers for different situations. Comparing them on the same pairs shows when each one helps:

from rapidfuzz import fuzz

pairs = [
    ("Apple Inc.", "Apple Incorporated"),
    ("New York Mets", "Mets"),
    ("Smith, John", "John Smith"),
    ("Starbucks Coffee #1042", "starbucks coffee"),
]
print(f"{'pair':<42}{'ratio':>7}{'partial':>9}{'token_sort':>12}{'WRatio':>8}")
for a, b in pairs:
    print(f"{a + ' | ' + b:<42}{fuzz.ratio(a, b):>7.1f}{fuzz.partial_ratio(a, b):>9.1f}"
          f"{fuzz.token_sort_ratio(a, b):>12.1f}{fuzz.WRatio(a, b):>8.1f}")

Output:

pair                                        ratio  partial  token_sort  WRatio
Apple Inc. | Apple Incorporated              64.3     94.7        64.3    85.5
New York Mets | Mets                         47.1    100.0        47.1    90.0
Smith, John | John Smith                     47.6     66.7        95.2    90.5
Starbucks Coffee #1042 | starbucks coffee    73.7     87.5        73.7    73.7
Command Prompt table comparing RapidFuzz ratio, partial_ratio, token_sort_ratio and WRatio on company, team, name and store strings
partial_ratio handles substrings, token_sort_ratio ignores word order, and WRatio blends several scorers.
ScorerBest for
fuzz.ratioWhole strings of similar length; typos
fuzz.partial_ratioOne string inside a longer one (“Mets” in “New York Mets”)
fuzz.token_sort_ratioSame words in a different order (“Smith, John”)
fuzz.WRatioA good general default when inputs vary

How do you find the best fuzzy match in a list?

process.extractOne() returns the best match, its score and its index. utils.default_process lowercases and strips punctuation first, which usually improves results:

from rapidfuzz import process, fuzz, utils

products = ["iPhone 15 Pro Max", "Samsung Galaxy S25", "Google Pixel 9", "iPad Air 13-inch", "MacBook Air 15"]
for query in ["iphone 15 pro", "galaxy s25", "pixle 9", "macbook air"]:
    match, score, idx = process.extractOne(query, products, scorer=fuzz.WRatio, processor=utils.default_process)
    print(f"{query:<14} -> {match:<20} score {score:.1f}")

print(process.extract("air", products, scorer=fuzz.partial_ratio, processor=utils.default_process, limit=2))

Output:

iphone 15 pro  -> iPhone 15 Pro Max    score 95.0
galaxy s25     -> Samsung Galaxy S25   score 90.0
pixle 9        -> Google Pixel 9       score 85.5
macbook air    -> MacBook Air 15       score 95.0
[('iPad Air 13-inch', 100.0, 3), ('MacBook Air 15', 100.0, 4)]
Command Prompt output of RapidFuzz process.extractOne matching messy product searches to a list of product names
Even “pixle 9” finds Google Pixel 9. process.extract() returns the top several matches.

This is exactly how your search boxes and product lookups can tolerate typos.

Deduplicating messy records with fuzzy matching

Your customer lists will collect near-duplicates: nicknames, typos, accents and “Last, First” order. Grouping names above a similarity threshold catches most of them:

from rapidfuzz import fuzz, utils
from rapidfuzz.distance import Levenshtein

customers = ["Jonathan Miller", "Jon Miller", "Jonathan Millar", "Maria Garcia",
             "María García", "Garcia, Maria", "Chris Lee"]

print("edit distance 'Miller' -> 'Millar':", Levenshtein.distance("Miller", "Millar"))

groups = []
for name in customers:
    for group in groups:
        if fuzz.token_sort_ratio(name, group[0], processor=utils.default_process) >= 85:
            group.append(name)
            break
    else:
        groups.append([name])
for g in groups:
    print(g)

Output:

edit distance 'Miller' -> 'Millar': 1
['Jonathan Miller', 'Jonathan Millar']
['Jon Miller']
['Maria Garcia', 'Garcia, Maria']
['María García']
['Chris Lee']
Command Prompt output of Python grouping near-duplicate customer names with RapidFuzz token_sort_ratio and a Levenshtein distance
The typo and the reversed name were grouped. The accented María García and the nickname Jon fell below 85, a reminder to review edge cases.

There’s no perfect threshold; I start around 85 and check a sample by hand. Stripping accents first with unicodedata.normalize('NFKD', ...) would catch María too.

For final decisions on things like customer accounts, keep a human in the loop.

Is RapidFuzz faster than difflib?

Yes, by a wide margin, because it’s written in C++. For a few comparisons you won’t notice; for matching thousands of records, you will:

import timeit
from difflib import SequenceMatcher
from rapidfuzz import fuzz

a, b = "123 North Lamar Boulevard, Austin, TX 78703", "123 N Lamar Blvd, Austin TX 78703"
t_diff = timeit.timeit(lambda: SequenceMatcher(None, a, b).ratio(), number=20_000)
t_rf = timeit.timeit(lambda: fuzz.ratio(a, b), number=20_000)
print(f"difflib:   {t_diff:.3f} s  (score {SequenceMatcher(None, a, b).ratio() * 100:.1f})")
print(f"rapidfuzz: {t_rf:.3f} s  (score {fuzz.ratio(a, b):.1f})")

Output:

difflib:   0.689 s  (score 86.8)
rapidfuzz: 0.005 s  (score 86.8)

The two libraries also score slightly differently, so don’t mix their thresholds. For other text cleanup before matching, see Python replace with regular expression and isalpha() in Python.

More string guides worth a look:

Frequently asked questions

What is fuzzy string matching in Python?

It measures how similar two strings are, so near-matches like typos or reordered words can still be matched. difflib and RapidFuzz are the common tools.

Should I use difflib or RapidFuzz?

difflib is built in and fine for small jobs. RapidFuzz is much faster and has more scorers, so it’s better for large lists.

What score threshold should I use?

Start around 80-90 for RapidFuzz scores, then check a sample of matches and adjust.

How do I ignore word order?

Use fuzz.token_sort_ratio or fuzz.token_set_ratio.

What happened to fuzzywuzzy?

It was renamed to thefuzz. RapidFuzz offers a compatible API and is faster, so most new projects use it.

Where is difflib documented?

See difflib in the Python docs.