You can use list.index() to find the first match of a string, for multiple matches you can use enumerate():
>>> tip = """# Regex tip
...
... Description (usually single line, could be more) ...
...
... ~~~
... >>> import re
... >>> words = ("how", "are", "you", "today")
... >>> for word in words:
... ... if re.match(r".*(o[uw])$", word): # 🤔
... ... word
... ...
... 'how'
... 'you'
... ~~~
...
... #regex"""
>>> lines = tip.splitlines()
>>> lines.index("~~~")
4
# assuming two ~~~ markers
>>> start_index, end_index = [
... i for i, line in enumerate(lines)
... if line.strip() == "~~~"
... ]
>>> start_index, end_index
(4, 13)
>>> from pprint import pprint as pp
>>> pp(lines[start_index + 1: end_index])
['>>> import re',
'>>> words = ("how", "are", "you", "today")',
'>>> for word in words:',
'... if re.match(r".*(o[uw])$", word): # 🤔',
'... word',
'...',
"'how'",
"'you'"]
#built-ins #slicing