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

Stephen Hansen apt.shansen at gmail.com
Fri Oct 23 23:21:04 EDT 2009


On Fri, Oct 23, 2009 at 7:31 PM, Peng Yu <pengyu.ut at gmail.com> wrote:

> For example, the long string is 'abcabc' and the given string is
> 'abc', then 'abc' appears 2 times in 'abcabc'. Currently, I am calling
> 'find()' multiple times to figure out how many times a given string
> appears in a long string. I'm wondering if there is a function in
> python which can directly return this information.
>

>>> 'abcabc'.count('abc')
2
>>> print ''.count.__doc__
S.count(sub[, start[, end]]) -> int

Return the number of non-overlapping occurrences of substring sub in
string S[start:end].  Optional arguments start and end are interpreted
as in slice notation.

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091023/6b2a6776/attachment.html>


More information about the Python-list mailing list