Python:how 查找子字符串在字符串中出现的次数?

Python:how to find the times of a substring's occurrence in a string?

查找给定字符串中所有出现的“USA”,忽略大小写

鉴于: str1 = "欢迎来到美国。美国很棒,不是吗?"

预期结果: 美国计数为:2

您可以使用 .lower() 将字符串小写,然后使用 .count() 查找 "usa"

的出现
str1 = "Welcome to USA. usa awesome, isn't it?"
print(str1.lower().count('usa'))

输出

2