文章详情页
Python count函数使用方法实例解析
浏览:81日期:2022-08-01 15:59:10
Python count()方法
描述
Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
count()方法语法:
str.count(sub, start= 0,end=len(string))
参数
sub -- 搜索的子字符串 start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。 end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。返回值
该方法返回子字符串在字符串中出现的次数。
以下实例展示了count()方法的实例:
实例(Python 2.0+)
#!/usr/bin/python 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)
以上实例输出结果如下:
str.count(sub, 4, 40) : 2str.count(sub) : 1
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章:
1. Python中的min及返回最小值索引的操作2. Python常用base64 md5 aes des crc32加密解密方法汇总3. Python使用Socket实现简单聊天程序4. js中textContent、innerText和innerHTML的用法以及区别5. Python+Appium实现自动抢微信红包6. Android如何用自定义View实现雪花效果7. springboot使用CommandLineRunner解决项目启动时初始化资源的操作8. Python操作MongoDb数据库流程详解9. SpringBoot框架打包体积简化过程图解10. Vue中强制组件重新渲染的正确方法
排行榜

网公网安备