how do I count spaces at the beginning of a string?

Steven Bethard steven.bethard at gmail.com
Wed May 16 23:19:08 EDT 2007


walterbyrd wrote:
> The strings start with whitespace, and have a '*' or an alphanumeric
> character. I need to know how many whitespace characters exist at the
> beginning of the string.

You really need to stop posting the same message multiple times.

A possible answer using regular expressions:

 >>> import re
 >>> whitespace_matcher = re.compile(r'^\s+')
 >>> match = whitespace_matcher.search('        abc def ghi')
 >>> len(match.group())
8

STeVe



More information about the Python-list mailing list