python中count()方法
2017-08-09 15:38
120 查看
Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
count()方法语法:
str.count(sub, start= 0,end=len(string))
返回子字符串在字符串中出现的次数
sub:搜索的子字符串
start:字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end:字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
运行结果:
当对象是一个嵌套的列表时,要查找嵌套列表中的列表参数count()方法同样可以完成
count()方法语法:
str.count(sub, start= 0,end=len(string))
返回子字符串在字符串中出现的次数
sub:搜索的子字符串
start:字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end:字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
str = "this is string example....wow!!!" sub = "i" print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40) sub = "wow" # 整体出现的次数 print "str.count(sub) : ", str.count(sub) sub='my' print "str.count(sub) : ", str.count(sub) sub='sw' print "str.count(sub) : ", str.count(sub)
运行结果:
str.count(sub, 4, 40) : 2 str.count(sub) : 1 str.count(sub) : 0 str.count(sub) : 0
当对象是一个嵌套的列表时,要查找嵌套列表中的列表参数count()方法同样可以完成
>>> ['a','iplaypython.com','c','b','a'].count('a') 2 >>> x = [1,2,'a',[1,2],[1,2]] >>> x.count([1,2]) 2 >>> x.count(1) 1 >>> x.count('a') 1 >>>
相关文章推荐
- 【人生苦短,我学Python】个人学习笔记——str.count方法
- Python count()方法和Python List count()方法
- 【Python】Python3 List count()方法
- Python 列表 count() 方法
- Python字符串处理之count()方法的使用
- python之count方法错误
- Python统计字符串中某字符出现的次数,count方法比正则表达式更快
- 关于Python中,re.sub(pattern, repl, string, count=0, flags=0)方法的个人理解
- Python 元组 count() 方法
- Python中List.count()方法的使用教程
- Python中List.count()方法的使用教程
- Python字符串处理之count()方法的使用
- Python count() 方法
- 获取python未知异常信息的方法
- python判断字符串(string)是否包含(contains)子字符串的方法
- Python学习总结(二)----python的练习方法
- Python rpartition() 方法
- python中requests使用代理proxies方法介绍
- Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
- Python列表推导式的使用方法