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
|
3455 Discussions
|
Please Login in order to post a comment
def count_substring(string, sub_string): sub = len(sub_string) counts = 0
if name == 'main': string = input().strip() sub_string = input().strip()
def count_substring(string, sub_string): c=s=0 while True: s=string.find(sub_string,s) if s==-1: break c+=1 s+=1 return c
sliding through string looking using index and counting occurences + comments for debug
def count_substring(string, sub_string):
count = 0 for i in range(len(string) - len(sub_string)+1):
if string[i:i + len(sub_string)] == sub_string: count += 1 return count