Starting with "aaafoobbb" and looking for "foo" and the surrounding two characters on either side, you could do:
>>> start_string = "aaafoobbb"
>>> search_string = "foo"
>>> index = start_string.index(search_string)
>>> s[(index - 2) : (index + len(search_string) + 2)]
Should be easy enough to adapt to your needs, although you'll need to add some extra checks to make sure your slice indices are within range (e.g. make sure that index - 2
is not less than 0). You definitely want to become more familiar with slicing and strings in Python.