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:12:45 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)
    >> 
    >> ? 

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

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

I retract my statement.  It's a bug in my code.  help("".lstrip) shows why:

    lstrip(...)
        S.lstrip([chars]) -> string or unicode

        Return a copy of the string S with leading whitespace removed.
        If chars is given and not None, remove characters in chars instead.
        If chars is unicode, S will be converted to unicode before stripping

Note the second sentence.

Sorry for the flub.

Skip





More information about the Python-list mailing list