第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > python count函数用法示例_python count函数用法详解

python count函数用法示例_python count函数用法详解

时间:2019-08-10 08:07:16

相关推荐

python count函数用法示例_python count函数用法详解

在python中可以使用“count()”函数统计字符串里某个字符出现的次数,该函数用于统计次数,其语法是“count(sub, start= 0,end=len(string))”,其参数sub表示搜索的子字符串。

Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

语法

count()方法语法:str.count(sub, start= 0,end=len(string))sub -- 搜索的子字符串

start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。

end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。