How to Find the First Match in String in Python


In this example, we will learn how to search the string to get the first occurrence of the match using the search() function in Python.

Source Code

import re

str1 = 'I love Python'

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

start_position = r.start()

print(start_position)

Output:

3
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement