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
More information about the Python-list mailing list
Mon Dec 22 17:06:44 EST 2003
- Previous message (by thread): Counting how many chars equal to a given char are in the beginning of a string
- Next message (by thread): Counting how many chars equal to a given char are in the beginning of a string
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
>>>>> "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
- Previous message (by thread): Counting how many chars equal to a given char are in the beginning of a string
- Next message (by thread): Counting how many chars equal to a given char are in the beginning of a string
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list