使用python find 函数,我想确定一个字符串是否有“=”符号。它可以找到除“=”之外的任何其他内容。
string: Math="Fun".
if (string.find("=") > -1):
有任何想法吗?
使用python find 函数,我想确定一个字符串是否有“=”符号。它可以找到除“=”之外的任何其他内容。
string: Math="Fun".
if (string.find("=") > -1):
有任何想法吗?
You can do this with the in
operator:
>>> "=" in "dog"
False
>>> "=" in "do=g"
True
There is no need to use str.find()
(or the deprecated string.find()
) to do this, unless you want to know the index of the character.
You can use the find
method directly on the string:
>>> "a = b".find("=")
2
Alternatively (though not as nice a method of doing so), you can use the find
method on the str
class:
>>> str.find("a = b", "=")
2
As Lattyware suggested, you ought to use the in
operator over this method, unless you need the index.