How do I check if a given Python string is a substring of another one?

PythonStringSubstring

Python Problem Overview


I have two strings and I would like to check whether the first is a substring of the other. Does Python have such a built-in functionality?

Python Solutions


Solution 1 - Python

Try using in like this:

>>> x = 'hello'
>>> y = 'll'
>>> y in x
True

Solution 2 - Python

Try

isSubstring = first in theOther

Solution 3 - Python

string.find("substring") will help you. This function returns -1 when there is no substring.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsnakileView Question on Stackoverflow
Solution 1 - PythonAndrew HareView Answer on Stackoverflow
Solution 2 - PythonMartin StoneView Answer on Stackoverflow
Solution 3 - PythonDaniel WehnerView Answer on Stackoverflow