Answers for "python find substring"

3

python find all elements of substring in string

def find_all(a_str, sub):
    start = 0
    while True:
        start = a_str.find(sub, start)
        if start == -1: return
        yield start
        start += len(sub) # use start += 1 to find overlapping matches

list(find_all('spam spam spam spam', 'spam')) # [0, 5, 10, 15]
Posted by: Guest on June-09-2020
4

find in python

def find_all_indexes(input_str, search_str):
    l1 = []
    length = len(input_str)
    index = 0
    while index < length:
        i = input_str.find(search_str, index)
        if i == -1:
            return l1
        l1.append(i)
        index = i + 1
    return l1


s = 'abaacdaa12aa2'
print(find_all_indexes(s, 'a'))
print(find_all_indexes(s, 'aa'))
Posted by: Guest on February-16-2020
0

Find substring into a string - Python

if "blah" in somestring: 
    continue
Posted by: Guest on May-29-2021

Code answers related to "python find substring"

Python Answers by Framework

Browse Popular Code Answers by Language