python search string code example
Example 1: python regular expression
import re
test_string = 'Hello greppers!'
pattern = re.compile(r'Hello')
match = pattern.finditer(test_string)
for match in matches:
print(match)
Example 2: find string in string python
>>> str = "Messi is the best soccer player"
>>> "soccer" in str
True
>>> "football" in str
False
Example 3: python .findall
match = re.search(r'bb', 'aabbcc')
match = re.search(r'cd', 'aabbcc')
match = re.search(r'...c', 'aabbcc')
match = re.search(r'\d\d\d', 'p123g')
match = re.search(r'\w\w\w', '@@abcd!!')
Example 4: find in python
def find_all_indexes(input_str, search_str):
l1 = []
length = len(input_str)
index = 0
while index < length:
i = input_str.find(search_str, index)
if i == -1:
return l1
l1.append(i)
index = i + 1
return l1
s = 'abaacdaa12aa2'
print(find_all_indexes(s, 'a'))
print(find_all_indexes(s, 'aa'))
Example 5: find on string in python
string.find(value, start, end)