Position of the found item in Python's search method
The match object can also be used to determine the position of the found substring. Two methods are used for this purpose. The start method returns the start position, and the end method returns the end position.
Let's try it in practice. Let's say we have a line:
txt = '123 456 789'
Let's find the first substring with numbers in it using the search method, and then output the position of the resulting substring using the start and end methods:
res = re.search('\d+', txt)
print(res.start()) # 0
print(res.end()) # 3
Given a string:
txt = 'abcde 123456'
Find a substring in it that consists only of letters. Print the position of the beginning and end of the found substring.