["python"]
What is the pythonic way to count the leading spaces in a string?

I know I can count the leading spaces in a string with this: >>> a = " foo bar baz qua \n" >>> print "Leading spaces", len(a) - len(a.lstrip()) Leading spaces 3 >>> But is there a more pythonic way?
Your way is pythonic but incorrect, it will also count other whitespace chars, to count only spaces be explicit a.lstrip(' '). Compare a = " \r\t\n\tfoo bar baz qua \n" print("Leading spaces", len(a) - len(a.lstrip())) >>> Leading spaces 7 and print("Leading spaces", len(a) - len(a.lstrip(' ')) >>> Leading spaces 3