Lesson 04
index() and count()
Learn how to locate the position of a substring with index() and count occurrences with count() using visual diagrams and memory tricks.
What you'll learn
| Topic | Focus |
|---|---|
index() |
Find the first position |
ValueError |
What happens when text is missing |
count() |
Count repeated characters or words |
| Difference | index() vs count() |
Visual roadmap
Think of these methods as answering two different questions.
1. index() → Find the first position
index() returns the starting position of the first match.
s = "python"
print(s.index("p")) # 0
print(s.index("o")) # 4
print(s.index("n")) # 5Memory:
index()answers "Where is it?"
2. Finding a word (substring)
index() also works with multiple characters.
s = "python is an oop lang"
print(s.index("is")) # 7
print(s.index("an")) # 10
print(s.index("thon")) # 2How Python calculates:
| p | y | t | h | o | n | (space) | i | s | |
|---|---|---|---|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
3. When the value is missing
If the substring doesn't exist, index() raises a ValueError.
s = "python"
s.index("K")
# ValueError: substring not foundWarning:
index()does not return-1; it throws an exception.
4. Finding every occurrence
index() finds only the first match. Use enumerate() to locate all matches.
s = "MISSISSIPPI"
for index, ch in enumerate(s):
if ch == "I":
print(index, "--->", ch)Output:
1 ---> I
4 ---> I
7 ---> I
10 ---> IMemory:
enumerate()gives(index, value)together.
5. count() → Count occurrences
count() tells you how many times something appears.
s = "MISSISSIPPI"
print(s.count("M")) # 1
print(s.count("S")) # 4
print(s.count("P")) # 2
print(s.count("I")) # 4Memory:
count()answers "How many?"
6. Counting in numbers
Numbers must first become a string.
n = 123321456234123412
s = str(n)
print(s.count("1")) # 4
print(s.count("2")) # 5Why str()?
s = "12345"
print(s.count("1")) # Correct
# print(s.count(1))
# TypeErrorcount() expects a string argument, not an integer.
7. index() vs count()
These methods solve different problems.
| Feature | index() |
count() |
|---|---|---|
| Purpose | Find position | Count occurrences |
| Returns | Integer index | Integer count |
| Not found | ValueError | 0 |
Playground example
s = "MISSISSIPPI"
print("index of I:", s.index("I"))
print("count M:", s.count("M"))
print("count S:", s.count("S"))
print("count I:", s.count("I"))
word = "python is an oop lang"
print("index 'is':", word.index("is"))
print("index 'an':", word.index("an"))Expected output:
index of I: 1
count M: 1
count S: 4
count I: 4
index 'is': 7
index 'an': 10Use the lesson playground at the bottom of the page to run this code live.
Quick knowledge check
-
What does
"python".index("o")return?
Answer:4 -
What happens if the substring is missing?
Answer:index()raisesValueError, whilecount()returns0. -
How many
"S"characters are in"MISSISSIPPI"?
Answer:4
30-second revision sheet
| Method | Easy memory |
|---|---|
index() |
Find the first position |
count() |
Count occurrences |
enumerate() |
Find every position |
Interview shortcut: Where? → index() · How many? → count() · Missing? → Error vs 0
Next: Strip Methods.
Loading playground…