difflib
The Python difflib module provides classes and functions for comparing sequences and generating human-readable difference reports. It implements the Ratcliff-Obershelp “gestalt pattern matching” algorithm for measuring sequence similarity and offers multiple output formats including unified diff, context diff, and HTML side-by-side tables:
>>> import difflib
>>> difflib.get_close_matches("pythn", ["python", "perl", "ruby"])
['python']
Key Features
- Computes similarity ratios between any two sequences with
SequenceMatcher - Generates unified diff output suitable for patch files with
unified_diff() - Produces human-readable line-by-line deltas with
Differandndiff() - Creates HTML tables for side-by-side comparison with
HtmlDiff - Finds the closest matches for a string from a list with
get_close_matches() SequenceMatcherworks on any hashable sequence type, not only lines of text
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
difflib.SequenceMatcher |
Class | Compares pairs of sequences and computes similarity ratios |
difflib.Differ |
Class | Generates human-readable delta output between sequences of lines |
difflib.HtmlDiff |
Class | Creates HTML tables for side-by-side line comparison with change highlights |
difflib.unified_diff() |
Function | Generates unified diff output with configurable context lines |
difflib.context_diff() |
Function | Generates context diff output in before/after block format |
difflib.ndiff() |
Function | Generates a Differ-style delta comparing two sequences of lines |
difflib.get_close_matches() |
Function | Returns the best fuzzy matches for a string from a list of candidates |
Examples
Computing the similarity ratio between two strings:
>>> import difflib
>>> sm = difflib.SequenceMatcher(None, "python", "pythoon")
>>> sm.ratio()
0.9230769230769231
Generating a unified diff between two lists of lines:
>>> import difflib
>>> before = ["one", "two", "three"]
>>> after = ["one", "TWO", "three", "four"]
>>> for line in difflib.unified_diff(before, after, lineterm=""):
... print(line)
---
+++
@@ -1,3 +1,4 @@
one
-two
+TWO
three
+four
Common Use Cases
The most common tasks for difflib include:
- Comparing two versions of a text file and reporting what changed
- Implementing fuzzy search or autocomplete by finding closest string matches
- Generating patch files or diff reports for version control workflows
- Displaying test failure details by comparing expected and actual output
- Building merge tools or change-tracking features in text editors
Real-World Example
A script can compare two versions of a configuration file and print a unified diff showing exactly what changed between them:
compare_configs.py
import difflib
v1 = """\
[server]
host = localhost
port = 8080
debug = true
""".splitlines(keepends=True)
v2 = """\
[server]
host = 0.0.0.0
port = 443
debug = false
workers = 4
""".splitlines(keepends=True)
diff = difflib.unified_diff(
v1, v2, fromfile="config_v1.ini", tofile="config_v2.ini"
)
print("".join(diff), end="")
Run the script from your command line. You’ll get an ouput like the following:
--- config_v1.ini
+++ config_v2.ini
@@ -1,4 +1,5 @@
[server]
-host = localhost
-port = 8080
-debug = true
+host = 0.0.0.0
+port = 443
+debug = false
+workers = 4
The output follows the standard unified diff format, making it directly usable as a patch file or for display in code review tools.
Related Resources
Tutorial
Reading and Writing Files in Python (Guide)
In this tutorial, you'll learn about reading and writing files in Python. You'll cover everything from what a file is made up of to which libraries can help you along that way. You'll also take a look at some basic scenarios of file usage as well as some advanced techniques.
For additional information on related topics, take a look at the following resources:
- Python's unittest: Writing Unit Tests for Your Code (Tutorial)
- Python Sequences: A Comprehensive Guide (Tutorial)
- Strings and Character Data in Python (Tutorial)
- Python's doctest: Document and Test Your Code at Once (Tutorial)
- Reading and Writing Files in Python (Course)
- Reading and Writing Files in Python (Quiz)
- Python's unittest: Writing Unit Tests for Your Code (Quiz)
- Python Sequences: A Comprehensive Guide (Quiz)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
By Leodanis Pozo Ramos • Updated April 2, 2026