当前位置:  首页>> 技术小册>> Python合辑2-字符串常用方法

20、count()

返回指定内容在字符串中出现的次数。

  1. n = 'hello world'.count('o')
  2. print(n)
  3. # 2
  4. n = 'hello world'.count('oo')
  5. print(n)
  6. # 0