0
Can anyone explain this code?
5 Respostas
+ 8
string.count(sub, i, i+len(sub))
is equivalent to
string[i:i+len(sub)] == sub
because slice length is never longer than sub length so it does somehing like this:
find('abcabcd', 'abc')
count += abc == abc
count += bca == abc
...
count += bcd == abc
+ 6
str.count(substr, start, end) returns non overlapping occurences of substr in str[start:end]
this function returns all occurences including overlapping so
'aaa'.count('aa') returns 1 but this function returns 2
+ 1
The function basically counts the number of occurrences of another smaller string in a larger string:
string = "this is a string"
sub_string = "is"
Number of occurrences = 2 # because there are two "is" in "this is a string"
However I see no purpose of that implementation because str.count(substring) already returns the number of occurrences
0
Mert Yazıcı i know but how does the code do that can you please explain
0
thanks