0

I was trying some question task was to find the number of times a substring appears in a given string. Given string was "ABCDCDC" and substring "CDC" I used the .count() method for string with gives me answer 1, which is wrong. So Please let me know the reason.

2 Answers2

0

But if the example is "ABCDCDC" , then count is right... if you search for "CDC" explicitly.

"AB cdc DC" so CDC is just once in a string.

0

The docs for str.count explicitly mentions this behavior:

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

See this answer for counting substrings with overlaps

Adam.Er8
  • 12,675
  • 3
  • 26
  • 38