Counting how many chars equal to a given char are in the beginning of a string

Skip Montanaro skip at pobox.com
Mon Dec 22 17:06:44 EST 2003


>>>>> "Jeff" == Jeff Epler <jepler at unpythonic.net> writes:

    Jeff> On Mon, Dec 22, 2003 at 03:16:41PM -0600, Skip Montanaro wrote:
    >> How about:
    >> 
    >> def howmanyatstart(s, pfx):
    >> return (len(s) - len(s.lstrip(pfx)))/len(pfx)
    >> 
    >> ?  This works for prefixes which are longer than a single character:
    >> 
    >> >>> howmanyatstart(">>>>message1", '>')
    >> 4
    >> >>> howmanyatstart("bobbobbob>>>>message1", 'bob')
    >> 3

    Jeff> strip() doesn't work that way:
    >>>> "bbbbbbbxxx".strip("bob")
    Jeff> 'xxx'

Then it looks like a bug in one or the other to me.

Skip





More information about the Python-list mailing list