Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding multiple occurrences of a string within a string in Python

Tags:

python

string

How do I find multiple occurrences of a string within a string in Python? Consider this:

>>> text = "Allowed Hello Hollow" >>> text.find("ll") 1 >>>  

So the first occurrence of ll is at 1 as expected. How do I find the next occurrence of it?

Same question is valid for a list. Consider:

>>> x = ['ll', 'ok', 'll'] 

How do I find all the ll with their indexes?

like image 962
user225312 Avatar asked Oct 06 '10 14:10

user225312


People also ask

How do you find multiple occurrences in a string in Python?

The finditer function of the regex library can help us perform the task of finding the occurrences of the substring in the target string and the start function can return the resultant index of each of them.

How do you find all occurrences of string in a string Python?

Use the string. count() Function to Find All Occurrences of a Substring in a String in Python. The string. count() is an in-built function in Python that returns the quantity or number of occurrences of a substring in a given particular string.

Which method finds the list of all occurrences of the pattern in a given string?

finditer() To get all occurrences of a pattern in a given string, you can use the regular expression method re. finditer(pattern, string) . The result is an iterable of match objects—you can retrieve the indices of the match using the match.


1 Answers

Using regular expressions, you can use re.finditer to find all (non-overlapping) occurences:

>>> import re >>> text = 'Allowed Hello Hollow' >>> for m in re.finditer('ll', text):          print('ll found', m.start(), m.end())  ll found 1 3 ll found 10 12 ll found 16 18 

Alternatively, if you don't want the overhead of regular expressions, you can also repeatedly use str.find to get the next index:

>>> text = 'Allowed Hello Hollow' >>> index = 0 >>> while index < len(text):         index = text.find('ll', index)         if index == -1:             break         print('ll found at', index)         index += 2 # +2 because len('ll') == 2  ll found at  1 ll found at  10 ll found at  16 

This also works for lists and other sequences.

like image 146
poke Avatar answered Sep 22 '22 01:09

poke