We were doing something like this last week thestring = "a_a_a_a_" >>> for x in range(len(thestring)): ... try: ... thestring.count("_a_", x, x + 3) ... except ValueError: ... pass