We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Strings
- Find a string
- Discussions
Find a string
Find a string
Sort by
recency
|
3394 Discussions
|
Please Login in order to post a comment
This is a simple solution.
def count_substring(string, sub_string): tot = 0 i = string.find(sub_string) if(i >= 0): tot += 1 if(i == -1): return tot else: while(i != -1): i = string.find(sub_string, i+1) if(i == -1): break else: tot += 1 return tot
Mam check this logic once please..
Happy with my code as it is easy to ready and digest
easier version
def count_substring(string, sub_string): counter = 0 while sub_string in string: counter += 1 find = string.find(sub_string) string = string[find + 1:] return counter