How to find common substring between two strings with Python?

Sometimes, we want to find common substring between two strings with Python.

In this article, we’ll look at how to find common substring between two strings with Python.

How to find common substring between two strings with Python?

To find common substring between two strings with Python, we can use the os.path.commonprefix method.

For instance, we write

import os
common = os.path.commonprefix(['apple pie available', 'apple pies'])

to call os.path.commonpredix with a list of strings to find the common substring between the 2 strings.

Then common should be 'apple pie'.

Conclusion

To find common substring between two strings with Python, we can use the os.path.commonprefix method.