I want to count how often a sequence multiple characters occurs in a string. For example, I want to see how many times ad
appears in badhadmad
. I thought about using a for
loop but I don't know how to iterate more than one character. How can I accomplish ths?
Asked
Active
Viewed 694 times
0

Adriaan
- 17,741
- 7
- 42
- 75

Minh Phúc Nguyễn
- 27
- 3
-
9How about simple `"badhadmad".count('ad')` :)? – Chris Aug 20 '19 at 07:24
-
1Possible duplicate of [Count number of occurrences of a given substring in a string](https://stackoverflow.com/questions/8899905/count-number-of-occurrences-of-a-given-substring-in-a-string) – Georgy Aug 20 '19 at 07:35
-
3@Chris But `"ababa".count('aba') == 1` :D? – TrebledJ Aug 20 '19 at 07:35
-
@TrebledJ True! `str.count` won't work in such cases. In that case, iteration is indeed one way: `sum(1 for i in range(len("ababa")) if "aba" == s[i:i+len("aba")])`. – Chris Aug 20 '19 at 07:43
3 Answers
2
Use this to count the number of occurrences of a sub-string in python.
s = "badhadmad"
print(s.count("ad"))
Using loop:
s = "badhadadmad"
l = len(s)
pattern = "ad"
n = len(pattern)
count = 0
for i in range(l - n):
if s[i] == pattern[0]:
if s[i:i+n] == pattern:
count += 1
print("Ans",count)
.count() gives only non-overlapping solution, for overlapping sub-strings:
import re
print(len(re.findall('(?=aba)', 'ababa')))

Yash
- 3,438
- 2
- 17
- 33
-
1
-
-
2probably a small one, but in Python readability counts :) Also count on Python to do the check efficiently. I'm sure that by doing `s[i:i+n] == pattern` if the first character doesn't match, no extra computation will be carried out – Tomerikoo Aug 20 '19 at 07:45
-
True that but I think readability is a subjective thing. Some find it redundant what other may find readable and vice versa. – Yash Aug 20 '19 at 07:47
-
1
1
Just use count
method over a string in python
x = "badhadmad"
x.count("ad")
# 3
Go through https://www.programiz.com/python-programming/methods/string/count for more usage of count method

Shrey
- 1,242
- 1
- 13
- 27
0
Generate character level n grams for your string str,
[str[i:i+n] for i in range(len(str)-n+1)]
and then use count if needed. You can loop over range of values of n you want to consider.

Deven
- 741
- 1
- 7
- 21