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:
= "How many 'a's are in this string which includes " \
s "the phrase 'I'm bananas about bananas'?"
= s.count('a')
n print(n) # prints 11
# Now let's check for the letter 'z'
= s.count('z')
n print(n) # prints 0, because there are no 'z's in s
You can count non-overlapping substrings in a string as well.
= "GACTGTAGACTTAGGGCTTAGAGTACTAGTAGCTGACTGACACG"
dna_fragment = dna_fragment.count("GTA")
n 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"""
= ("One morning, when Gregor Samsa woke from "
metamorphosis "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.")
= metamorphosis.count("Gregor")
n print(n) # prints 1
= metamorphosis.count("bed")
n print(n) # prints 2
= metamorphosis.count("he")
n 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.
= [9, 0, 3, 1, 3, 2, 5, 8, 9, 7, 1, 0, 2, 2, 4, 0, 5, 1]
lst = lst.count(1)
n print(n) # prints 3
= lst.count(9)
n print(n) # prints 2
= lst.count(8)
n print(n) # prints 1
= lst.count(6)
n print(n) # prints 0
It works with tuples also.
= ('Bob', 'Charlene', 'Bob', 'Alice', 'Alice', 'Dolores')
t = t.count('Bob')
n print(n) # prints 2
= t.count('Charlene')
n print(n) # prints 1
= t.count("Egbert")
n print(n) # print 0
Keep in mind what counts as an element in a sequence.
= (('a', 97), ('b', 98), ('c', 99), ('d', 100), ('e', 101))
t
= t.count(('c', 99))
n print(n) # prints 1
= t.count('c')
n print(n) # prints 0
= t.count(99)
n 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.