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

Oren Tirosh oren-py-l at hishome.net
Mon Dec 22 19:41:38 EST 2003


On Mon, Dec 22, 2003 at 12:04:21PM -0800, Stormbringer wrote:
> Hi,
> 
> Given a string s and a char c, is there a short & elegant way to
> determine how many consecutive occurences of c are in the beginning of
> s ?
> 
> For example if s = ">>>>message1" and c = ">" then the number I am
> looking for is 4 (the string begins with 4 '>').

re.match('>*', s).end()

   Oren





More information about the Python-list mailing list