码迷,mamicode.com
首页 > 编程语言 > 详细

python count()

时间:2018-01-18 17:06:05      阅读:489      评论:0      收藏:0      [点我收藏+]

标签:print   mos   stdin   module   err   string   erro   most   span   

 

 

count()

描述

Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

语法

count()方法语法:

str.count(sub, start= 0,end=len(string))

参数

  • sub -- 搜索的子字符串
  • start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
  • end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
>>> a = [mike,mike,ben,tom,jack]
>>>
>>> a.count()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: count() takes exactly one argument (0 given)
>>>
>>> a.count(mike)
2


>>> a.count(tom)
1

 

python count()

标签:print   mos   stdin   module   err   string   erro   most   span   

原文地址:https://www.cnblogs.com/mingerlcm/p/8310324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!