直接上代码:
方案一用find方法:
def find_all(str1, date, start, end):
while start <= end:
i = str1.find(date, start, end)
if i == -1:
break
start = i+1
print(i)
str1 = '好主意。好主意,好主意'
start = 0
end = len(str1)
date = '好'
find_all(str1, date, start, end)
运行结果:
===================== RESTART: C:/Users/hzf/Desktop/1.py =====================
0
4
8
>>>
方法二用正则表达式:
import re
str1 = '好主意,好主意,好主意'
for each in re.finditer('好', str1):
print(each.start())
运行结果:
>>> import re
>>> str1 = '好主意,好主意,好主意'
>>> for each in re.finditer('好', str1):
print(each.start())
0
4
8