count函数用于统计字符串里某个子字符串出现的次数。
语法
S.count(sub[, start[, end]]) -> int
参数
返回值
示例
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.