Module 7 · Strings — topic 16 of 22
count()
count(sub) returns how many times a substring occurs. Learn non-overlapping counting, start and end limits, case-insensitive counting, and counting words versus characters.
Learning objectives
- Count occurrences of a substring
- Count case-insensitively and within a range
- Distinguish substring counts from whole-word counts
Definition
count(sub) returns how many times a substring occurs. Learn non-overlapping counting, start and end limits, case-insensitive counting, and counting words versus characters.
Introduction
count(sub) scans a string and returns the number of non-overlapping occurrences of sub. "banana".count("a") is 3.
Why it matters
Counting characters and substrings answers many questions quickly: how many vowels, how many commas in a line, how often a keyword appears, how many times a mistake was made.
Detailed explanation
A count of zero is a meaningful answer, not an error, which makes count() safe to call on any text without checking first.
count() is a quick way to check the structure of text before processing it. For example, a valid email address contains exactly one @, a time written as hh:mm:ss contains exactly two colons, and a sentence usually ends with exactly one full stop. Checking these counts early lets a program reject malformed input with a clear message.
The start and end arguments follow slice rules, so text.count("a", 0, 5) counts only within the first five characters and never raises an index error.
For frequencies of every character or word in a text, calling count() repeatedly is slow because each call scans the whole string. collections.Counter, covered in Module 15, counts everything in one pass.
Occurrences are counted from left to right without overlapping: "aaaa".count("aa") is 2, not 3.
count(sub, start, end) counts only within that slice of the string.
Counting is case-sensitive; lower-case the text first for case-insensitive counts.
count("") returns len(s) + 1, which is rarely useful, so avoid empty substrings.
A substring count is not a word count: "the" is counted inside “other” and “there”. To count whole words, split first and use the list’s count method or a Counter.
Lists also have a count() method that counts equal items.
Syntax
- count returns an int.
- Lower-case first to ignore case.
- Split first to count whole words.
Code examples
Counting substrings
3 2 2 2
Case-insensitive
1 3
More examples
Vowels and whole words
Vowels: 15 Substring 'the': 5 Word 'the': 2 Commas in CSV line: 3
Step-by-step explanation
- The generator counts each vowel separately and sum adds the counts.
- As a substring, “the” is found inside other, there and theatre too, giving 5.
- Splitting into words and counting the list gives the 2 real occurrences of the word.
- Counting commas tells you a CSV line has 4 fields.
Expected output
Running “Vowels and whole words” prints exactly:
Practical applications
- Writing tools count words and characters.
- Data validation checks that each CSV line has the expected number of separators.
- SEO tools count keyword occurrences.
Best practices
- Lower-case before counting text.
- Split into words for word counts.
- Use collections.Counter (Module 15) to count many different items.
Common mistakes
Counting words with str.count
Avoid
Prefer
Substring counting matches inside longer words.
Forgetting case
Avoid
Prefer
count is case-sensitive.
Key takeaways
- count returns the number of non-overlapping occurrences.
- It is case-sensitive and counts inside words.
- Split first to count whole words.
Try it yourself
- 1Count the spaces in a sentence and use it to estimate the number of words.
- 2Count how many times the letter s appears in "Mississippi", ignoring case.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
17 questions cover the “Strings” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?