import re # sub result = re.sub(r'India', 'the World', 'AV is largest Analytics community of India') print (result) # compile pattern = re.compile('AV') result = pattern.findall('AV Analytics Vidhya AV') print (result) result2 = pattern.findall('AV is largest analytics community of India') print (result2) # search patterns = [ 'this', 'that', 'h th' ] text = 'Does this text match the pattern?' for pattern in patterns: print ('Looking for "%s" in "%s" ->' % (pattern, text), ) if re.search(pattern, text): print ( 'found a match!' ) else: print ( 'no match' )