Home >Backend Development >Python Tutorial >How to Discover Common Substrings in Python using difflib?
Comparing two strings to identify their common substring is a fundamental task in string manipulation. This comprehensive guide showcases a Python-based solution utilizing the difflib module to perform this operation efficiently.
Using difflib's Find_Longest_Match Method
The difflib module, part of Python's standard library, provides an array of sequence-comparison utilities. Among them, find_longest_match finds the longest common substring between two strings.
Consider two strings:
string1 = "apple pie available" string2 = "come have some apple pies"
To find their common substring using find_longest_match:
from difflib import SequenceMatcher match = SequenceMatcher(None, string1, string2).find_longest_match() print(match) # -> Match(a=0, b=15, size=9) print(string1[match.a:match.a + match.size]) # -> apple pie print(string2[match.b:match.b + match.size]) # -> apple pie
In this example, the output is:
Match(a=0, b=15, size=9) apple pie apple pie
This indicates that the longest common substring is "apple pie," and both strings share this substring.
Compatibility with Python Versions
For Python versions prior to 3.9, the find_longest_match() method requires slightly different arguments:
SequenceMatcher(None, string1, string2).find_longest_match(0, len(string1), 0, len(string2))
The above is the detailed content of How to Discover Common Substrings in Python using difflib?. For more information, please follow other related articles on the PHP Chinese website!