python中count()函数的用法

python字符串函数用法大全链接

count()函数

描述:统计字符串里某个字符出现的次数。可以选择字符串索引的起始位置和结束位置。           

语法:str.count("char", start,end)  或 str.count("char")    -> int    返回整数

  • str —— 为要统计的字符(可以是单字符,也可以是多字符)。
  • star —— 为索引字符串的起始位置,默认参数为0。
  • end —— 为索引字符串的结束位置,默认参数为字符串长度即len(str)。

程序示例:

str = "i love python,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

 

猜你喜欢

转载自blog.csdn.net/qq_40678222/article/details/83033466