This question already has answers here:
Count number of occurrences of a substring in a string
(36 answers)
Closed 24 days ago.
Given:
st = "banana"
print(st.count("ana"))
# => 1
How come the count is 1, it should be 2 right? please explain to me.
As per documentation of str.count():
Return the number of non-overlapping occurrences of substring sub ...
In your case, the occurrences of "ana" are overlapping.
Related
This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 10 months ago.
eg: "ABC.sample.int.int01" like that, only want to check this string contains 3 dots.
Thanks
You can check it like this, with count attribute of str in python. If it is equal to 3, then your strings actually contains exactly 3 dots.
if 'ABC.sample.int.int01'.count('.') == 3:
...
else:
...
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 1 year ago.
I tried to create a program which returns the number of times a certain string occurs in the main string.
main_string="ABCDCDC"
find_string="CDC"
print(main_string.count(find_string))
Output=1
....
But there are 2 CDC. Is there any other ways to solve?
Try using regex:
print(len(re.findall(fr"(?={find_string})", main_string)))
Or try using this list comprehension:
x = len(find_string)
print(len([main_string[i:x + i] for i in range(len(main_string)) if main_string[i:x + i] == find_string]))
Both codes output:
2
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
How String.count() works? [duplicate]
(2 answers)
Python string count not working properly? [duplicate]
(2 answers)
Closed 4 years ago.
I'm having fun with some challenges and one of them makes me count substrings in a string. I have a problem specifically with "banana":
str = "banana"
print(str.count("ana"))
This should return 2 because "ana" appears two times:
b a n a n a
a n a
a n a
But str.count("ana") returns only 1. I've also tried with regexp:
import re
str = "banana"
print(len(re.findall("ana", str)))
But it also returns 1. Am I missing something?
thank you!
Yes, you are missing something.
str.count(): Return the number of (non-overlapping) occurrences of substring sub in string s
This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 6 years ago.
I'm trying to find a fast algorithm for counting how many times a substring is found in a string using Python. I know there are some builtin functions for doing that but they do no serve my propose. For example, the word "ana" appears 2 times in "banana" but the string method count just returns 1.
The code I have so far is:
s = "banana"
sub = "ana"
count = 0
for i in range(4):
if s.startswith(sub):
count += 1
If some one knows a better way, please let me know.
possible this:
s = "banana"
sub = "ana"
count = len(s.split(sub))-1
This question already has answers here:
How can I find the number of overlapping sequences in a String with Python? [duplicate]
(4 answers)
Closed 6 years ago.
There are two occurrences of 'aba' in 'ababa' (0th index and 2nd index):
myString = 'ababa'
print(myString.count('aba'))
Yet this code outputs a value of: 1
I know this issue seems really simple, but shouldn't the answer be 2 here?
If not, then isn't the count function not really doing what it's supposed to?
Is there a simple alternative?
From the Python string function documentation
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.
count does not count overlapping occurrences.
If you want to count overlapping occurrences you can use regex with a lookahead assertion:
import re
print(len(re.findall('(?=aba)', 'ababa')))
Documentation to the rescue: https://docs.python.org/2/library/string.html
Return the number of (non-overlapping) occurrences of substring sub in string s[start:end]. Defaults for start and end and interpretation of negative values are the same as for slices.