Python difflib Module
Example
Find a close match for a word:
import difflib
words = ["ape", "apple", "peach", "puppy"]
print(difflib.get_close_matches("appel", words, n=1))
Try it Yourself »
Definition and Usage
The difflib module helps compare sequences, generate deltas, and find close matches.
Use it for file comparisons, human-readable diffs, and approximate string matching.
Members
| Member | Description |
|---|---|
| context_diff() | Generate a context diff. |
| Differ | Compare sequences and produce human-friendly deltas. |
| get_close_matches() | Return a list of close matches to a word. |
| HtmlDiff | Create HTML side-by-side comparison tables. |
| ndiff() | Compare sequences and generate a delta (with intraline hints). |
| restore() | Generate one of the original sequences from a delta. |
| SequenceMatcher | Compute similarities between sequences. |
| unified_diff() | Generate a unified diff. |