实现一个函数count_char
时间: 2024-05-04 16:21:36 浏览: 50
PHP count_chars()函数讲解
可以计算一个字符串中某个字符出现的次数。
函数定义如下:
```python
def count_char(s: str, c: str) -> int:
"""
计算字符串s中字符c出现的次数
:param s: 要计算的字符串
:param c: 要统计的字符
:return: 字符c在字符串s中出现的次数
"""
count = 0
for char in s:
if char == c:
count += 1
return count
```
示例:
```python
s = "hello world"
c = "l"
count = count_char(s, c)
print(count) # 输出 3
```
阅读全文