Count method
Counting elements within a sequence
Sometimes we have a sequence—string, list, or tuple—and we’d like to know how many times a particular element occurs in the sequence. Python supplies a .count() method for each of these types. This method takes an argument and returns the number of times the value of the argument appears in the underlying sequence.
Examples:
s = "How many 'a's are in this string which includes " \
"the phrase 'I'm bananas about bananas'?"
n = s.count('a')
print(n) # prints 11
# Now let's check for the letter 'z'
n = s.count('z')
print(n) # prints 0, because there are no 'z's in sYou can count non-overlapping substrings in a string as well.
dna_fragment = "GACTGTAGACTTAGGGCTTAGAGTACTAGTAGCTGACTGACACG"
n = dna_fragment.count("GTA")
print(n) # prints 3 (check for yourself)Another example:
"""Source of quote: The Metamorphosis by Franz Kafka,
translated by David Wyllie, via Project Gutenberg:
https://www.gutenberg.org/cache/epub/5200/pg5200.txt"""
metamorphosis = ("One morning, when Gregor Samsa woke from "
"troubled dreams, he found himself transformed "
"in his bed into a horrible vermin. He lay "
"on his armour-like back, and if he lifted "
"his head a little he could see his brown "
"belly, slightly domed and divided by arches "
"into stiff sections. The bedding was "
"hardly able to cover it and seemed ready "
"to slide off any moment. His many legs, "
"pitifully thin compared with the size of "
"the rest of him, waved about helplessly as "
"he looked.")
n = metamorphosis.count("Gregor")
print(n) # prints 1
n = metamorphosis.count("bed")
print(n) # prints 2
n = metamorphosis.count("he")
print(n) # prints 11
# Doesn't count "He" (case-sensitive) but does count "he"
# in "when", "head", "arches", "the", and "helplessly".It works with lists too.
lst = [9, 0, 3, 1, 3, 2, 5, 8, 9, 7, 1, 0, 2, 2, 4, 0, 5, 1]
n = lst.count(1)
print(n) # prints 3
n = lst.count(9)
print(n) # prints 2
n = lst.count(8)
print(n) # prints 1
n = lst.count(6)
print(n) # prints 0It works with tuples also.
t = ('Bob', 'Charlene', 'Bob', 'Alice', 'Alice', 'Dolores')
n = t.count('Bob')
print(n) # prints 2
n = t.count('Charlene')
print(n) # prints 1
n = t.count("Egbert")
print(n) # print 0Keep in mind what counts as an element in a sequence.
t = (('a', 97), ('b', 98), ('c', 99), ('d', 100), ('e', 101))
n = t.count(('c', 99))
print(n) # prints 1
n = t.count('c')
print(n) # prints 0
n = t.count(99)
print(n) # prints 0 The last two calls to .count() return zero because neither 'c' nor 99 are elements of t. The elements of t are tuples, e.g., ('c', 99).
Comprehension check
Given the string:
s = "amanaplanacanalpanama", what is the evaluation ofs.count('a')?Given the same string, what is the evaluation of
count('ma')?Does this work?
count(s, 'p')? Why or why not?Given this tuple,
t = ('cheddar', 'gouda', 'munster'), what is the evaluation oft[0].count('d')?
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.