Categories
Python Answers

How to find common substring between two strings with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *