Linux 拨号vps windows公众号手机端

python如何用count函数

lewis 6年前 (2019-03-16) 阅读数 8 #程序编程
文章标签 count函数python

在python中使用count函数的方法

count:count()函数的作用是用于统计字符串里某个字符出现的次数。

count()函数语法:

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

参数:

sub:需要搜索的子字符串。

start :字符串开始搜索的位置。

end:字符串中结束搜索的位置。

count()函数使用方法:

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) : 2

str.count(sub) : 1

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门