Php 什么是strpos的python等价物($elem,text)!=(错误)

Php 什么是strpos的python等价物($elem,text)!=(错误),php,python-2.7,strpos,Php,Python 2.7,Strpos,python的等价物是什么: if (strpos($elem,"text") !== false) { // do_something; } 未找到时返回-1: 未找到时引发ValueError: 要简单地测试子字符串是否在字符串中,请使用: needle in haystack 这相当于以下PHP: strpos(haystack, needle) !== FALSE 从可以看出,使用“in”的代码真的很漂亮吗 最后两次打印也一样,由您选择。欢迎使用堆栈溢出,请下次使用搜索

python的等价物是什么:

if (strpos($elem,"text") !== false) {
    // do_something;  
}
未找到时返回-1: 未找到时引发ValueError: 要简单地测试子字符串是否在字符串中,请使用:

needle in haystack
这相当于以下PHP:

strpos(haystack, needle) !== FALSE

可以看出,使用“in”的代码真的很漂亮吗


最后两次打印也一样,由您选择。

欢迎使用堆栈溢出,请下次使用搜索!谢谢你的忍者快速反应。但是,我在使用.find:AttributeError:'list'对象没有属性'find'时收到此错误。是否应该导入模块以使用.find语法这正是我正在使用的。但是我收到了以下错误:AttributeError:'list'对象没有属性'find'。我想您是在搜索对象,而不是字符串。如果要将字符串搜索到对象(带字符串),则应使用循环。检查并将其更新到您的代码:
如果“这是字符串示例…哇!!!”。查找(“考试”)!=-1:印刷“作品”AttributeError:“Response”对象没有“find”属性。谢谢您提供的解决方案。为了澄清,我使用了
如果大海捞针:
我爱上了
大海捞针
。来自PHP的Python代码非常容易让人阅读。。。
needle in haystack
strpos(haystack, needle) !== FALSE
if elem.find("text") != -1:
    do_something
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))