Q> Suppose the count function for a string didn't exist. Define a function that returns the number of non-overlapping occurrences of a sub string in a string.
I think this problem means that if I type the string "abcd"
then the result is 10?
I guess the substrings would be:
a
b
c
d
ab
bc
cd
abc
bcd
abcd
So the result is 10. Is it right?
Given the count function for a string counts the occurrences of a specific substring, it seems like the answer would attempt to mimic it rather than measure every possible substring. You can use the re module to accomplish a count of a substring. The findall function returns a list of matches, and the length can be used to find the count
import re
x ='thetheitem1thetheitem2'
len(re.findall(r'the',x))
4
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.