什么是strpos的python等价物($ elem,"text")!== false)

Cos*_*ech 11 php strpos python-2.7

什么是python相当于:

if (strpos($elem,"text") !== false) {
    // do_something;  
}
Run Code Online (Sandbox Code Playgroud)

xda*_*azz 32

找不到时返回-1:

pos = haystack.find(needle)
pos = haystack.find(needle, offset)
Run Code Online (Sandbox Code Playgroud)

找不到时引发ValueError:

pos = haystack.index(needle)
pos = haystack.index(needle, offset)
Run Code Online (Sandbox Code Playgroud)

要简单地测试子字符串是否在字符串中,请使用:

needle in haystack
Run Code Online (Sandbox Code Playgroud)

这相当于以下PHP:

strpos(haystack, needle) !== FALSE
Run Code Online (Sandbox Code Playgroud)

来自http://www.php2python.com/wiki/function.strpos/