This question already has an answer here:
这个问题已经有了答案:
- Does Python have a string 'contains' substring method? 14 answers
- Python有一个字符串“contains”子字符串方法吗?14个答案
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有这样的内置功能吗?
3 个解决方案
#1
374
Try using in
like this:
试着这样使用:
>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
#2
39
Try
试一试
isSubstring = first in theOther
#3
27
string.find("substring")
will help you. This function returns -1
when there is no substring.
string.find(“字符串”)将帮助你。当没有子字符串时,该函数返回-1。
#1
374
Try using in
like this:
试着这样使用:
>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
#2
39
Try
试一试
isSubstring = first in theOther
#3
27
string.find("substring")
will help you. This function returns -1
when there is no substring.
string.find(“字符串”)将帮助你。当没有子字符串时,该函数返回-1。