What is the python equivalent of strpos($elem,"text") !== false)

Cosco Tech picture Cosco Tech · Jun 17, 2013 · Viewed 29.8k times · Source

What is the python equivalent of:

if (strpos($elem,"text") !== false) {
    // do_something;  
}

Answer

xdazz picture xdazz · Jun 17, 2013

returns -1 when not found:

pos = haystack.find(needle)
pos = haystack.find(needle, offset)

raises ValueError when not found:

pos = haystack.index(needle)
pos = haystack.index(needle, offset)

To simply test if a substring is in a string, use:

needle in haystack

which is equivalent to the following PHP:

strpos(haystack, needle) !== FALSE

From http://www.php2python.com/wiki/function.strpos/