Format: string.count(sub, start= 0,end=len(string))
string = "Add Grepper Answer"
print(string.count('e')
>>> 3
# 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)
"""Find all occurrences of element in list"""
# If you simply want find how many times an element appears
# use the built-in function count()
def find_occur(lst, item):
return lst.count(item)
# Test -----------------------------------------------------------
print(find_occur([None, None, 1, 2, 3, 4, 5], None)) # 2
# If you wanna find where they occur instead
# - This returns the indices where element is found within a list
def find(lst, item):
return [i for (i, x) in enumerate(lst) if x == item]
# Test Code ------------------------------------------------------
from random import randint, choice
lst = [randint(0, 99) for x in range(10)] # random lst
item = choice(lst) # item to find
found = find(lst, item) # lst of where item is found at
print(f"lst: {lst}",
f"item: {item}",
f"found: {found}",
sep = "
")
it counts the number of elements in the list or in the string(words)
>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4
import time
cando = True
number = 10
while cando:
print(number)
number = number - 1
time.sleep(1)
if number == 0:
cando = False
a = 'have a nice day'
symbol = 'abcdefghijklmnopqrstuvwxyz'
for key in symbol:
print(key, a.count(key))