python字符串-计数(count)
2022/4/5 20:19:04
本文主要是介绍python字符串-计数(count),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
count函数用于统计字符串里某个子字符串出现的次数。
语法
S.count(sub[, start[, end]]) -> int
参数
- sub: 搜索的子字符串。
- start: 可选参数,开始搜索的位置。
- end: 可选参数,结束搜索的位置。
返回值
- 子字符串在字符串中出现的次数。
示例
str = 'abcabcd' print('统计单个字符出现的次数:', str.count('a')) print('统计单个字符出现的次数:', str.count('a', 1)) print('统计单个字符出现的次数:', str.count('a', 1, 3)) print('统计子字符串出现的次数:', str.count('ab')) print('统计子字符串出现的次数:', str.count('df'))
统计单个字符出现的次数: 2 统计单个字符出现的次数: 1 统计单个字符出现的次数: 0 统计子字符串出现的次数: 2 统计子字符串出现的次数: 0
help()
Help on built-in function count: count(...) method of builtins.str instance S.count(sub[, start[, end]]) -> int Return the number of non-overlapping occurrences of substring sub in string S[start:end]. Optional arguments start and end are interpreted as in slice notation.
这篇关于python字符串-计数(count)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python