How to Get Position of First Match Occurrence in Python


In this example, we will learn how to get the position (start- and end-position) of the first match occurrence when searching for a string in Python.

Source Code

import re

str1 = 'I love Python'

r = re.search('o', str1)

print(r.span())

Output:

(3, 4)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement