def count(word, s):
word = word.upper()
s = s.upper()
res = 0
for i in word:
if i == s:
res += 1
return res
注释:s转换为大写必须在for循环前实现,如果写到if条件中,则返回的结果始终为0
def count(word, s):
frequency = {}
word = word.upper()
s = s.upper()
for w in word:
if w not in frequency:
frequency[w] = 1
else:
frequency[w] += 1
return frequency[s]
print(count(word = "Imahandsomeboy", s='o'))
def count(word,s):
word = word.upper()
s = s.upper()
res = word.count(s)
return res
print(count("hellowordsss", 's'))