python: find first string in string

Viewed 479

Given a string and a list of substrings I want to the first position any substring occurs in the string. If no substring occurs, return 0. I want to ignore case.

Is there something more pythonic than:

given = 'Iamfoothegreat'
targets = ['foo', 'bar', 'grea', 'other']
res = len(given)
for t in targets:
    i = given.lower().find(t)
    if i > -1 and i < res:
        res = i

if res == len(given):
    result = 0
else:
    result = res

That code works, but seems inefficient.

5 Answers
Related