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
Instead only the 'abcd' can match, and matches the leftmost 'abcd' in the second sequence: >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") >>> s.find_longest_match(0, 5, 0, 9) Match(a=1, b=0, size=4)
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › difflib.SequenceMatcher.html
difflib.SequenceMatcher — Python Standard Library
Example, comparing two strings, and considering blanks to be “junk”: >>> s = SequenceMatcher(lambda x: x == " ", ... "private Thread currentThread;", ...
🌐
HexDocs
hexdocs.pm › difflib › Difflib.SequenceMatcher.html
Difflib.SequenceMatcher — Difflib v0.1.0
For example, pass fn x -> x == " " if you're comparing lines as sequences of characters, and don't want to synch up on blanks or hard tabs. auto_junk - Optional parameter autojunk should be set to false to disable the "automatic junk heuristic" that treats popular elements as junk.
🌐
GitHub
github.com › python › cpython › blob › main › Lib › difflib.py
cpython/Lib/difflib.py at main · python/cpython
t = get_theme(force_no_color=True).difflib · · _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm) started = False · for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n): if not started: started = True ·
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 - Although this library may not be as famous as other third-party libraries (such as diff in git), difflib is a very useful and powerful tool when dealing with text comparison and merging. Combined with the simplicity and flexibility of Python, it is still particularly important in many situations. 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).
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › compare-sequences-in-python-using-dfflib-module
Compare sequences in Python using dfflib module - GeeksforGeeks
February 24, 2021 - # import required module import difflib # assign parameters par1 = 'Geeks for geeks!' par2 = 'geeks' # compare matches = difflib.SequenceMatcher( None, par1, par2).get_matching_blocks() for ele in matches: print(par1[ele.a:ele.a + ele.size])
Find elsewhere
🌐
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 · × ·
🌐
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 ...
🌐
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.
🌐
SourceForge
epydoc.sourceforge.net › stdlib › difflib.SequenceMatcher-class.html
difflib.SequenceMatcher - Epydoc - SourceForge
Methods: __init__(isjunk=None, a='', b='') Construct a SequenceMatcher. set_seqs(a, b) Set the two sequences to be compared. set_seq1(a) Set the first sequence to be compared. set_seq2(b) Set the second sequence to be compared. find_longest_match(alo, ahi, blo, bhi) Find longest matching block ...
🌐
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 ... 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 ...
🌐
Medium
ajinkya29.medium.com › what-is-difflib-41649066591c
What is Difflib?. So let's get started with this amazing… | by Ajinkya Mishrikotkar | Medium
June 14, 2021 - import difflib a = 'Medium' b = 'Median' seq = difflib.SequenceMatcher(None,a,b) d = seq.ratio()*100 print(d) 66.66666666666666
🌐
lxml
lxml.de › 3.1 › api › private › difflib.SequenceMatcher-class.html
difflib.SequenceMatcher
Methods: __init__(isjunk=None, a='', b='') Construct a SequenceMatcher. set_seqs(a, b) Set the two sequences to be compared. set_seq1(a) Set the first sequence to be compared. set_seq2(b) Set the second sequence to be compared. find_longest_match(alo, ahi, blo, bhi) Find longest matching block ...
🌐
Python
docs.python.org › 2.4 › lib › sequencematcher-examples.html
4.4.2 SequenceMatcher Examples
October 18, 2006 - Previous: 4.4.1 SequenceMatcher Objects Up: 4.4 difflib Next: 4.4.3 Differ Objects
🌐
Kite
kite.com › python › docs › difflib.SequenceMatcher
Code Faster with Line-of-Code Completions, Cloudless Processing
Kite is a free autocomplete for Python developers. Code faster with the Kite plugin for your code editor, featuring Line-of-Code Completions and cloudless processing.
🌐
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 difflib’s documentation, it is based on a variant of https://en.m.wikipedia.org/wiki/Gestalt_Pattern_Matching This algorithm calculates string similarity based on the length of the longest common subsequence and recursive lengths of common characters in other parts ...