How do I check if a given Python string is a subst

2019-01-29 19:21发布

问题:

This question already has an answer here:

  • Does Python have a string 'contains' substring method? 17 answers

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?

回答1:

Try using in like this:

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


回答2:

Try

isSubstring = first in theOther


回答3:

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