python中count函数的用法-Python教程

资源魔 30 0

python中count函数的用法

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) :  2
str.count(sub) :  1

保举学习:Python视频教程

以上就是python中count函数的用法的具体内容,更多请存眷资源魔其它相干文章!

标签: Python python教程 python编程 python使用问题

抱歉,评论功能暂时关闭!