Difference between re.search and re.match in Python?


re.match() searches for matches from the beginning of a string while re.search() searches for matches anywhere in the string.

Example:

import re

claim = 'People love Python.'

print(re.search(r'Python', claim).group())
# => Python

print(re.match(r'Python', claim))
# => None

print(re.search(r'People', claim).group())
# => People

print(re.match(r'People', claim).group())
# => People