python count()
时间: 2023-11-16 19:06:28 浏览: 49
python中count函数简单用法
The `count()` method in Python is used to count the number of occurrences of a specified element in a list or a string.
Syntax:
```
list.count(element)
string.count(str)
```
Example:
```
# Using count() on a list
fruits = ['apple', 'banana', 'orange', 'apple']
count_apple = fruits.count('apple')
print(count_apple) # Output: 2
# Using count() on a string
text = "Hello, World!"
count_l = text.count('l')
print(count_l) # Output: 3
```
阅读全文