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

Question:

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?

Asked By: snakile

||

Answers:

Try

isSubstring = first in theOther
Answered By: Martin Stone

Try using in like this:

>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
Answered By: Andrew Hare

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

Answered By: Daniel Wehner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.