Adventures in Machine Learning

Unlocking Common Substrings: Techniques to Find Matches in Python

Unlocking the Secrets of Finding a Common Substring Between Two Strings in Python

Python is widely renowned for its ease of use, vast library of functions, and considerable flexibility. One of the most common tasks in programming is finding strings that match or overlap each other.

This is where common substrings come into play, allowing us to identify matching sequences within larger strings. This article delves into various ways of finding common substrings between two strings in Python.

Using SequenceMatcher Class

One of the most straightforward approaches to identify a common substring is by utilizing the SequenceMatcher class, readily available in Python’s difflib library. This class compares two strings and returns an object detailing their similarities.

Using its find_longest_match method, we can obtain the index of the longest matching substring. The index helps to isolate the substring, which can then be extracted as shown below:


from difflib import SequenceMatcher

string1 = “Hello World!”

string2 = “World, how are you doing today?”

matcher = SequenceMatcher(None, string1, string2)

match = matcher.find_longest_match(0, len(string1), 0, len(string2))

common_substring = string1[match.a: match.a + match.size]



In this case, the result is the substring “World!” since it is common to both strings.

Removing Leading and Trailing Whitespace

Suppose two strings have common substrings, but they might differ in their leading and trailing whitespace. In that case, we can use the str.strip() method to remove any unnecessary whitespace that might prevent us from identifying the common substrings.

For example:


string1 = ” Hello World! “

string2 = ” World, how are you doing today? “

stripped_string1 = string1.strip()

stripped_string2 = string2.strip()

matcher = SequenceMatcher(None, stripped_string1, stripped_string2)

match = matcher.find_longest_match(0, len(stripped_string1), 0, len(stripped_string2))

common_substring = stripped_string1[match.a: match.a + match.size]



The output is still “World!”, since the leading and trailing spaces have been removed. Using os.path.commonprefix Method

Another useful method to find the longest common prefix between two strings is the os.path.commonprefix method.

This method, as the name suggests, is more commonly used to find the common prefix for two file paths. However, it can also work for regular strings as shown below:


import os

string1 = “Hello World!”

string2 = “Hello to the World!”

common_substring = os.path.commonprefix([string1, string2])



Here, the output is “Hello “, which is the longest common prefix between the two strings.

Using set() Class and intersection() Method

We can convert each string into a set of its substrings and then use the intersection() method to obtain the common substring. Here’s how it works:


string1 = “Hello World!”

string2 = “World, how are you doing today?”

set1 = {string1[i: j] for i in range(len(string1)) for j in range(i + 1, len(string1) + 1)}

set2 = {string2[i: j] for i in range(len(string2)) for j in range(i + 1, len(string2) + 1)}

common_substrings = set1.intersection(set2)



The result of this code is {‘World’, ‘orld’, ‘!’}, which is not entirely what we need. We only want to find the longest common substring.

We can, therefore, use the max() function and pass in the key=len function to find the longest common substring:


longest_common_substring = max(common_substrings, key=len)



Finally, the output will be ‘World,’ which is the longest common substring between the two strings.

Using List Comprehension

Another simple way to find the common substrings between two strings is by using a list comprehension and the in operator. Let’s take a look:


string1 = “Hello World!”

string2 = “World, how are you doing today?”

common_substring = [string1[i:j] for i in range(len(string1)) for j in range(i+1,len(string1)+1) if string1[i:j] in string2]



This code will produce [‘World’, ‘!’], which are the common substrings between the two strings.

Using For Loop

We can also use a simple for loop to iterate through each character in both strings while appending any matching substring to a new string. Here’s how it goes:


string1 = “Hello World!”

string2 = “World, how are you doing today?”

result = ”

for i in range(len(string1)):

for j in range(i+1, len(string1)):

if string1[i:j] in string2:

if len(string1[i:j]) > len(result):

result = string1[i:j]



In this case, the output is “World,” since it is the longest common substring between the two strings.

Additional Resources

In conclusion, several methods can be used to identify common substrings between two strings in Python. Understanding how these methods work and their use cases will form an excellent foundation for solving any related programming tasks.

For further reference and more detailed explanations, you can refer to the following resources:

– Python documentation on SequenceMatcher:

– Python documentation on os.path.commonprefix:

– Python documentation on set() class and intersection() method:

– A comprehensive guide on finding common substrings in Python:

In conclusion, finding common substrings between two strings is a crucial task in programming, and there are various ways of accomplishing it in Python. Using the SequenceMatcher class and the os.path.commonprefix method are straightforward and efficient methods, while the set() class, list comprehension, and for loop provide alternatives to suit individual preferences.

The importance of understanding these techniques and their use cases cannot be overstated, as they form a solid foundation for solving related programming tasks. By implementing the methods discussed in this article, programmers can efficiently and effectively identify common substrings between strings, which is beneficial in many real-world coding scenarios.

Popular Posts