如何检查给定的Python字符串是否是另一个字符串的子字符串?(复制)

时间:2022-12-10 07:07:57

This question already has an answer here:

这个问题已经有了答案:

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。