擅长:python、mysql、java
<p>实际情况是,<code>name.find("Sac")</code>返回<code>0</code>,它的布尔值为<code>False</code>,因此<code>if</code>-子句不会按您想要的方式触发它们。在</p>
<p>试试这个:</p>
<pre><code>name = "Sachin"
if "chin" in name:
print("Text present")
print(name)
if "Sac" in name:
print("Sac is present")
print(name)
</code></pre>
<p>注意:<code>0</code>由<code>name.find("Sac")</code>返回,因为<code>"Sachin"</code>处的<code>"Sac"</code>从位置0开始(Python使用0-index)。在</p>
<p>除了0-index之外,Python还区分大小写,这意味着<code>name.find("sac")</code>返回{<cd10>}(这是当您在<code>str</code>中尝试<code>find</code>的内容是<strong>而不是</strong>时发生的)。但是,<code>bool(-1) = 1</code>所以<code>if</code>最终触发并执行其下面的代码。在</p>