Answers for "find in python"

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 python

def find_all_indexes2(input_str, search_str):
    indexes = []
    index = input_str.find(search_str)

    while index != -1:
        indexes.append(index)
        index = input_str.find(search_str, index+1)

    return indexes

print(find_all_indexes2("Can you can a can as a canner can can a can", "can"))
# returns [8, 14, 23, 30, 34, 40]
# Note: Capital "Can" was not found
Posted by: Guest on April-04-2021

Python Answers by Framework

Browse Popular Code Answers by Language