Last modified: Dec 19, 2025 By Alexander Williams

Fix AttributeError: 'str' object has no attribute 'count'

Python's AttributeError is a common exception. It signals an attribute issue.

The error "'str' object has no attribute 'count'" is confusing. The count() method exists for strings.

This error usually means you are not calling count() on a string. You are likely calling it on a list.

Understanding the Error Message

The error message has two key parts. First, it mentions a 'str' object.

This suggests Python thinks your variable is a string. The second part says it has no 'count' attribute.

This is the confusing part. String objects do have a count() method.

The contradiction means your variable is not a string when the code runs. It is likely a list.

Common Cause: Calling count() on a List

The most frequent cause is simple. You intend to use a string method on a list.

You might have a list of strings. You then try to call count() on the entire list.

The count() method for lists counts element occurrences. It is not the same as the string method.

You must call it on an individual string inside the list, not the list itself.


# Example causing the error
my_list = ["apple", "banana", "apple"]
result = my_list.count("a")  # ERROR: 'list' object has no attribute 'count'?
# Wait, lists have count(). The real error is different.
# Let's look at a more accurate example.
    

The above comment shows a nuance. Lists do have a count() method.

The real error occurs when you mistakenly treat a list as a string. See the next example.


# The actual common error scenario
my_data = ["hello", "world"]  # This is a list
# Later, you incorrectly assume my_data is a string
character_count = my_data.count("l")  # AttributeError!
    

Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'list' object has no attribute 'count'

Notice the traceback says 'list' object. The initial error title can be misleading.

Your debugging might have you convinced the variable is a string. But it is a list.

How to Diagnose and Fix the Error

Follow these steps to find and solve the problem.

Step 1: Check the Variable Type

Use the type() function. Print the type of your variable right before the error line.


my_variable = ["this", "is", "a", "list"]
print(type(my_variable))  # Check the type
print(my_variable)
count = my_variable.count("i")  # This line will fail
    

['this', 'is', 'a', 'list']
Traceback (most recent call last):
  AttributeError: 'list' object has no attribute 'count'

The output clearly shows my_variable is a list. You cannot use string methods on it.

Step 2: Ensure You Are Using a String

If you need to count characters in a string, ensure you have a string. Access the correct element if it's in a list.


# Correct approach for a list of strings
word_list = ["apple", "berry", "cherry"]
# To count 'p's in the first word, index the list
count_p = word_list[0].count("p")
print(f"Count of 'p' in '{word_list[0]}': {count_p}")

# If you need a single string, join the list
big_string = " ".join(word_list)
count_r = big_string.count("r")
print(f"Count of 'r' in the joined string: {count_r}")
    

Count of 'p' in 'apple': 2
Count of 'r' in the joined string: 3

Step 3: Review Variable Assignments

The error often comes from earlier code. A variable meant to be a string becomes a list.

Trace back through your code. Look for where the variable gets its value.

Maybe a function returns a list instead of a string. Or an assignment is wrong.

Other Related AttributeErrors

This error is part of a family. Mixing object types causes many AttributeErrors.

For example, trying to use clear() on an integer causes a similar error. Learn more about fixing AttributeError 'int' object has no attribute 'clear'.

Another common mistake is with dictionary methods. See our guide on AttributeError 'dict' object has no attribute 'clear' for clarity.

Errors with the copy() method are also frequent. Check out how to resolve AttributeError 'list' object has no attribute 'copy'.

Best Practices to Avoid the Error

Use clear variable names. Names like word_list or sentence_string help.

Add type hints in your code. They make expected types clear to you and tools.

Write small functions. Each function should do one thing with a clear input and output type.

Test often. Run your code after small changes to catch type errors early.

Conclusion

The AttributeError about 'str' and 'count' is a type confusion error. Your variable is not a string.

It is most likely a list or another collection. Use type() to confirm the variable's type.

Then, adjust your code. Either access a string inside the list or convert the list to a string.

Understanding object types is key to debugging in Python. This skill helps fix many similar errors.