xxxxxxxxxx
it counts the number of elements in the list or in the string(words)
xxxxxxxxxx
Format: string.count(sub, start= 0,end=len(string))
string = "Add Grepper Answer"
print(string.count('e')
>>> 3
xxxxxxxxxx
1
2
3
my_list = [1, 2, 2, 3, 4, 2, 5, 2]
count = my_list.count(2) print(count)
# Output: 4
Copied!
xxxxxxxxxx
>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4
xxxxxxxxxx
# Python program to count Even
# and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)