Hello! I am working on problem 2, where I need to make a function that checks strings and returns a tuple of the indexes that match the "key". Here's my code: def subStringMatchExact(target, key): match = [] index = 0 for i in target: if i == key: match.append(index) index += 1 else: index += 1 tuple(match) #convert list to tuple return match This code works fine so long as the key is only a single character. How can I modify this so that the function will return matches for longer strings? Thanks!
Check out the find() function
for i in target: if i == key: Just for fun, put "print i" in between these lines and run it with a not too long target.
As suggested, you can use a built-in function or you can also consider taking every possible bit of the target string where the bit's length equals the length of the key and testing for equality. One such bit would be: target[ i + len(key) ]
just work with tuple: tuple=() tuple= tuple[:count]+(i,) for example cont= 1 i=4 ("",4,)
Join our real-time social learning platform and learn together with your friends!