Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner18 min

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.

index() answers Where? and count() answers How many?

1. index() → Find the first position

index() returns the starting position of the first match.

python indexed — index("o") = 4
python
s = "python"

print(s.index("p"))   # 0
print(s.index("o"))   # 4
print(s.index("n"))   # 5

Memory: index() answers "Where is it?"

2. Finding a word (substring)

index() also works with multiple characters.

python is an oop lang — index("is") = 7
python
s = "python is an oop lang"

print(s.index("is"))    # 7
print(s.index("an"))    # 10
print(s.index("thon"))  # 2

How 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.

Searching for K in python raises ValueError
python
s = "python"

s.index("K")
# ValueError: substring not found

Warning: 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.

MISSISSIPPI — I positions at 1, 4, 7, 10
python
s = "MISSISSIPPI"

for index, ch in enumerate(s):
    if ch == "I":
        print(index, "--->", ch)

Output:

text
1 ---> I
4 ---> I
7 ---> I
10 ---> I

Memory: enumerate() gives (index, value) together.

5. count() → Count occurrences

count() tells you how many times something appears.

MISSISSIPPI — count("S") = 4
python
s = "MISSISSIPPI"

print(s.count("M"))   # 1
print(s.count("S"))   # 4
print(s.count("P"))   # 2
print(s.count("I"))   # 4

Memory: count() answers "How many?"

6. Counting in numbers

Numbers must first become a string.

Number to str() to count("1") = 4
python
n = 123321456234123412

s = str(n)

print(s.count("1"))   # 4
print(s.count("2"))   # 5

Why str()?

python
s = "12345"

print(s.count("1"))   # Correct

# print(s.count(1))
# TypeError

count() 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
index() Where? vs count() How many?

Playground example

python
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:

text
index of I: 1
count M: 1
count S: 4
count I: 4
index 'is': 7
index 'an': 10

Use the lesson playground at the bottom of the page to run this code live.

Quick knowledge check

  1. What does "python".index("o") return?
    Answer: 4

  2. What happens if the substring is missing?
    Answer: index() raises ValueError, while count() returns 0.

  3. How many "S" characters are in "MISSISSIPPI"?
    Answer: 4

30-second revision sheet

Search methods hub — index vs count, missing value behavior
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…