Computer >> Computer tutorials >  >> Programming >> Python

What is the search() function in Python?


In Python, search() is a method of the module re.

Syntax of search()

re.search(pattern, string):

It is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only. Unlike in re.match() method, here searching for pattern ‘Tutorials’ in the string ‘TP Tutorials Point TP’ will return a match.

Example

import re
result = re.search(r'Tutorials', 'TP Tutorials Point TP')
print result.group(0)

Output

Tutorials

Here you can see that, search() method is able to find a pattern from any position of the string but it only returns the first occurrence of the search pattern.