什么是python等价物:
if (strpos($elem,"text") !== false) {
// do_something;
}
pos = haystack.find(needle)
pos = haystack.find(needle, offset)
pos = haystack.index(needle)
pos = haystack.index(needle, offset)
要简单地测试子字符串是否在字符串中,请使用:
needle in haystack
这等效于以下 PHP:
strpos(haystack, needle) !== FALSE
if elem.find("text") != -1:
do_something
使用“in”的代码真的很漂亮吗:
in_word = 'word'
sentence = 'I am a sentence that include word'
if in_word in sentence:
print(sentence + 'include:' + word)
print('%s include:%s' % (sentence, word))
最后 2 次打印做同样的事情,你选择。