Find a string

Sort by

recency

|

3394 Discussions

|

  • + 0 comments

    This is a simple solution.

    def count_substring(string, substring):
    
    times = 0
    
    for pos, char in enumerate(string):
    
        if(char == sub_string[0] and string[pos:pos + len(sub_string)] == sub_string):
            times += 1         
    
    return times
    
  • + 0 comments
    import re
    
    def count_substring(string, sub_string):
        iters = re.findall(rf'(?=({re.escape(sub_string)}))', string)
        return len(iters)
    
  • + 0 comments

    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..

  • + 0 comments

    Happy with my code as it is easy to ready and digest

    def count_substring(string, sub_string):
        x = len(string)
        y = len(sub_string)
        count = 0
        for i in range(0, x):
            if sub_string == string[ i : i+y ]:
                count += 1
        return count
    
  • + 0 comments

    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