python中函数count()的功能是统计字符串里某个字符出现的次数,语法为【str.count("char", start,end)】,其中str为要统计的字符,star为索引字符串的起始位置,end为索引字符串的结束位置。
count()函数
描述:统计字符串里某个字符出现的次数。可以选择字符串索引的起始位置和结束位置。
语法:str.count("char", start,end) 或 str.count("char") -> int 返回整数
str —— 为要统计的字符(可以是单字符,也可以是多字符)。
star —— 为索引字符串的起始位置,默认参数为0。
end —— 为索引字符串的结束位置,默认参数为字符串长度即len(str)。
程序示例:
str = "i love <a style='color:#f60; text-decoration:underline;' href="//m.sbmmt.com/zt/15730.html" target="_blank">python</a>,i am learning python" print(str.count("i")) #star 和end 为默认参数 print(str.count("i",2)) # star值为2,end值为默认参数 print(str.count("i",2,5)) #star值为2,end值为5 print(str.count("am")) #多字符统计
程序运行结果:
3 2 0 1
相关免费学习推荐:python视频教程
以上就是python中函数COUNT()的功能是什么的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号