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.
🌐
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
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).
🌐
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
🌐
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 ... 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 ...
🌐
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
February 19, 2021 - 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.
🌐
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 › difflib.SequenceMatcher
python - SequenceMatcher Explained: Word-Level Diffs, Junk Handling, and Fuzzy Matching
The SequenceMatcher constructor accepts an optional isjunk parameter—a function that returns True if a sequence element should be considered "junk" and ignored in the comparison. import difflib text_a = "Hello, World!" text_b = "Hello World" # No ...
🌐
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 ...
🌐
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
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › difflib.SequenceMatcher.__init__.html
difflib.SequenceMatcher.__init__ — Python Standard Library
difflib.SequenceMatcher.__init__ View page source · SequenceMatcher.__init__(isjunk=None, a='', b='', autojunk=True)[source]¶ · Construct a SequenceMatcher. Optional arg isjunk is None (the default), or a one-argument function that takes a sequence element and returns true iff the element is junk. None is equivalent to passing “lambda x: 0”, i.e. no elements are considered to be junk. For example, pass ·
🌐
Basicexamples
basicexamples.com › example › python › difflib-sequencematcher
Basic example of difflib.SequenceMatcher in Python
import difflib # Initialize two strings string1 = "Hello World" string2 = "Hello Python" # Create a SequenceMatcher object sm = difflib.SequenceMatcher(None, string1, string2) # Get the ratio of similarity between the strings similarity_ratio = sm.ratio() print(similarity_ratio)
🌐
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
🌐
Pynerds
pynerds.com › python-difflib-module
Python difflib module
February 28, 2024 - The class actually uses the SequenceMatcher class for the comparisons. #import the Differ class from difflib import Differ #sample Texts text1 = "Half a pound of tuppenny rice".splitlines() text2 = "Half a pound of treacle".splitlines() #Instantiate a Differ object d = Differ() #Compare the two texts result = list(d.compare(text1, text2)) #print the results print('\n'.join(result))