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

The Question :

312 people think this question is useful

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?

The Question Comments :

The Answer 1

491 people think this answer is useful

Try using in like this:

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

The Answer 2

53 people think this answer is useful

Try

isSubstring = first in theOther

The Answer 3

38 people think this answer is useful

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

Add a Comment