Python字符串 计数() 函数是python编程语言中的一个内置函数,返回给定字符串中子字符串的出现次数。
null
语法:
一串计数(子字符串,开始=…,结束=…)
参数:
- count()函数有一个强制参数和两个可选参数。
- 强制参数:
- substring–要查找其计数的字符串。
- 可选参数:
- 启动(可选) –在搜索开始的字符串中开始索引。
- 结束(可选) –搜索结束的字符串内的结束索引。
返回值:
count()方法返回一个表示 次数 子字符串出现在给定的字符串中。
示例1:count()方法的实现 没有可选参数
Python3
# Python program to demonstrate the use of # count() method without optional parameters # string in which occurrence will be checked string = "geeks for geeks" # counts the number of times substring occurs in # the given string and returns an integer print (string.count( "geeks" )) |
输出:
2
示例2:count()方法的实现 使用可选参数
Python3
# Python program to demonstrate the use of # count() method using optional parameters # string in which occurrence will be checked string = "geeks for geeks" # counts the number of times substring occurs in # the given string between index 0 and 5 and returns # an integer print (string.count( "geeks" , 0 , 5 )) print (string.count( "geeks" , 0 , 15 )) |
输出:
1 2
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END