Python has string.find()
and string.rfind()
to get the index of a substring in string.
I wonder, maybe there is something like string.find_all()
which can return all founded indexes (not only first from beginning or first from end)?
For example:
string = "test test test test"
print string.find('test') # 0
print string.rfind('test') # 15
#that's the goal
print string.find_all('test') # [0,5,10,15]
You can try :
Here's a (very inefficient) way to get all (i.e. even overlapping) matches:
If you're just looking for a single character, this would work:
Also,
My hunch is that neither of these (especially #2) is terribly performant.
please look at below code
You can easily use:
https://www.programiz.com/python-programming/methods/string/count
Cheers!
You can use
re.finditer()
for non-overlapping matches.but won't work for: