You forgot the first parameter to SequenceMatcher.

>>> import difflib
>>> 
>>> a='abcd'
>>> b='ab123'
>>> seq=difflib.SequenceMatcher(None, a,b)
>>> d=seq.ratio()*100
>>> print d
44.4444444444

http://docs.python.org/library/difflib.html

Answer from Lennart Regebro on Stack Overflow
🌐
Python
docs.python.org › 3 › library › difflib.html
difflib — Helpers for computing deltas
This is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses SequenceMatcher both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines.
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › difflib.SequenceMatcher.html
difflib.SequenceMatcher — Python Standard Library
difflib.SequenceMatcher · View page source · class difflib.SequenceMatcher(isjunk=None, a='', b='', autojunk=True)[source]¶ · SequenceMatcher is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable.
🌐
HexDocs
hexdocs.pm › difflib › Difflib.SequenceMatcher.html
Difflib.SequenceMatcher — Difflib v0.1.0
SequenceMatcher tries to compute a "human-friendly diff" between two sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the longest contiguous & junk-free matching subsequence.
🌐
TestDriven.io
testdriven.io › tips › 6de2820b-785d-4fc1-b107-ed8215528f49
Tips and Tricks - Python - Using SequenceMatcher.ratio() to find similarity between two strings | TestDriven.io
https://docs.python.org/3/library/difflib.html#sequencematcher-objects · For example: from difflib import SequenceMatcher first = "Jane" second = "John" print(SequenceMatcher(a=first, b=second).ratio()) # => 0.5 · View All Tips · Feedback · × ·
🌐
GitHub
github.com › python › cpython › blob › main › Lib › difflib.py
cpython/Lib/difflib.py at main · python/cpython
· Function get_close_matches(word, possibilities, n=3, cutoff=0.6): Use SequenceMatcher to return list of the best "good enough" matches. · Function context_diff(a, b): For two lists of strings, return a delta in context diff format.
Author   python
🌐
Medium
medium.com › @zhangkd5 › a-tutorial-for-difflib-a-powerful-python-standard-library-to-compare-textual-sequences-096d52b4c843
A Tutorial of Difflib — A Powerful Python Standard Library to Compare Textual Sequences | by Kaidong Zhang | Medium
January 27, 2024 - You can complete a lot of text comparison work without leaving the Python environment. SequenceMatcher is a class in difflib that can be used to compare the similarity between two sequences (such as strings).
🌐
Educative
educative.io › answers › what-is-sequencematcher-in-python
What is SequenceMatcher() in Python?
SequenceMatcher is a class that is available in the difflib Python package.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › compare-sequences-in-python-using-dfflib-module
Compare sequences in Python using dfflib module - GeeksforGeeks
February 24, 2021 - Python3 · # import required module import difflib # assign parameters par1 = 'gfg' par2 = 'GFG' # compare print(difflib.SequenceMatcher(None, par1, par2).ratio()) Output: 0.0 · The get_matching_blocks() method of this class returns a list of triples describing matching subsequences.
🌐
SourceForge
epydoc.sourceforge.net › stdlib › difflib.SequenceMatcher-class.html
difflib.SequenceMatcher - Epydoc - SourceForge
The same idea is then applied ... sequences, but does tend to yield matches that "look right" to people. SequenceMatcher tries to compute a "human-friendly diff" between two sequences....
🌐
Coderz Column
coderzcolumn.com › tutorials › python › difflib-simple-way-to-find-out-differences-between-sequences-file-contents-using-python
difflib - Simple Way to Find Out Differences Between Sequences / File Contents using Python by Sunny Solanki
Our code for this example first creates an instance of SequenceMatcher using two sequences of integers. It then finds out the longest common subsequence using find_longest_match() and prints it. It then finds out the list of common subsequences using get_matching_blocks() and prints them. import difflib l1 = [1,2,3,5,6,7, 8,9] l2 = [2,3,6,7,8,10,11] seq_mat = difflib.SequenceMatcher(a=l1, b=l2) match = seq_mat.find_longest_match(alo=0, ahi=len(l1), blo=0, bhi=len(l2)) print("============ Longest Matching Sequence ==================") print("\nMatch Object : {}".format(match)) print("Matching S
🌐
lxml
lxml.de › 3.1 › api › private › difflib.SequenceMatcher-class.html
difflib.SequenceMatcher
SequenceMatcher is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are hashable. The basic algorithm predates, and is a little fancier than, an algorithm published in the late 1980's by Ratcliff and Obershelp under the hyperbolic name "gestalt ...
🌐
PyPI
pypi.org › project › cdifflib
cdifflib · PyPI
from cdifflib import CSequenceMatcher import difflib difflib.SequenceMatcher = CSequenceMatcher import library_that_uses_difflib # Now the library will transparantely be using the C SequenceMatcher - other # things remain the same library_that_uses_difflib.do_some_diffing()
      » pip install cdifflib
    
Published   Jan 13, 2025
Version   1.2.9
🌐
Python Module of the Week
pymotw.com › 2 › difflib
difflib – Compare sequences - Python Module of the Week
The SequenceMatcher class compares two sequences of any types, as long as the values are hashable. It uses an algorithm to identify the longest contiguous matching blocks from the sequences, eliminating “junk” values that do not contribute to the real data. import difflib from difflib_data ...
🌐
Quora
quora.com › What-algorithm-is-Pythons-difflib-SequenceMatcher-based-on
What algorithm is Pythons' difflib SequenceMatcher based on? - Quora
Answer (1 of 2): According to ... This algorithm calculates string similarity based on the length of the longest common subsequence and recursive lengths of common characters in other parts ......
🌐
Runebook.dev
runebook.dev › en › docs › python › library › difflib › sequencematcher-examples
SequenceMatcher Secrets: Dealing with Junk, Speed, and Readable Diffs in Python
SequenceMatcher can be slow, especially when comparing two very long strings, as its complexity can approach O(N×M) in the worst-case scenario (where N and M are the lengths of the sequences). import difflib import time s1_long = "The quick brown fox jumps over the lazy dog " * 1000 s2_long = "The quick brown fox leaps over the sleepy dog " * 1000 # Using the full ratio (accurate but slow) start = time.time() sm = difflib.SequenceMatcher(None, s1_long, s2_long) full_ratio = sm.ratio() end = time.time() print(f"Full Ratio ({end-start:.4f}s): {full_ratio:.3f}") # Using a quicker ratio (faster but less accurate) start = time.time() quick_ratio = sm.quick_ratio() end = time.time() print(f"Quick Ratio ({end-start:.4f}s): {quick_ratio:.3f}")
🌐
GitHub
github.com › mduggan › cdifflib
GitHub - mduggan/cdifflib: Python difflib with parts reimplemented in C
from cdifflib import CSequenceMatcher import difflib difflib.SequenceMatcher = CSequenceMatcher import library_that_uses_difflib # Now the library will transparantely be using the C SequenceMatcher - other # things remain the same library_that_uses_difflib.do_some_diffing()
Starred by 40 users
Forked by 7 users
Languages   C 65.3% | Python 32.7% | Makefile 2.0% | C 65.3% | Python 32.7% | Makefile 2.0%
🌐
Runebook.dev
runebook.dev › en › docs › python › library › difflib › difflib.SequenceMatcher
python - SequenceMatcher Explained: Word-Level Diffs, Junk Handling, and Fuzzy Matching
difflib.SequenceMatcher is a class within Python's built-in difflib module that's designed to compare pairs of sequences (which are often strings of text, but can be lists of any hashable objects) to find the longest contiguous matching subsequence.