-2

使用python find 函数,我想确定一个字符串是否有“=”符号。它可以找到除“=”之外的任何其他内容。

string: Math="Fun".

if (string.find("=") > -1):

有任何想法吗?

4

2 回答 2

8

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.

于 2012-05-10T15:30:54.847 回答
1

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.

于 2012-05-10T15:30:29.283 回答