Is there a command that returns the number of substrings in a string?

Gabriel Genellina gagsl-py2 at
Tue Oct 27 09:09:07 CET 2009

En Tue, 27 Oct 2009 04:31:22 -0300, Gerard Flanagan <grflanagan at>  

> alex23 wrote:
>> Gerard Flanagan <grflana... at> wrote:
>>> def count(text, *args):
>>  Other than the ability to handle multiple substrings, you do realise
>> you've effectively duplicated str.count()?
> I realise that calling this count function with a single argument would  
> be functionally identical to calling str.count(), yes. But I can imagine  
> the situation of wanting to find multiple (disjoint) substrings. Is  
> there a reason for preferring multiple calls to str.count() in such a  
> case? Or is there a more obvious approach?

There is a more efficient algorithm (Aho-Corasick) which computes all  
occurences of a set of substrings inside a given string at once. It  
*should* be faster than repeatly calling str.find for every substring, and  
faster than using regular expressions too. (note that you get not only the  
count of occurences, but their positions too).
I've seen a couple implementations for Python; if they're *actually*  
faster is to be determined...

Gabriel Genellina

More information about the Python-list mailing list