擅长:python、mysql、java
<pre><code>a = input("random string: ")
word = input("Wanted word: ")
count = 0
for i in range(len(a)-len(word)):
if a[i:i+len(word)] == word:
count += 1
print(count)
</code></pre>
<p>如果希望搜索不区分大小写,则可以使用<code>lower()</code>函数:</p>
<pre><code>a = input("random string: ").lower()
word = input("Wanted word: ").lower()
count = 0
for i in range(len(a)):
if a[i:i+len(word)] == word:
count += 1
print(count)
</code></pre>
<p>对于用户输入</p>
<pre><code>Hi Bob. This is bob
</code></pre>
<p>第一种方法将输出<code>1</code>,第二种方法将输出<code>2</code></p>