What is the pythonic way to count the leading spaces in a string?

前端 未结 6 1754
-上瘾入骨i
-上瘾入骨i 2020-12-03 13:10

I know I can count the leading spaces in a string with this:

>>> a = \"   foo bar baz qua   \\n\"
>>> print \"Leading spaces\", len(a) - le         


        
6条回答
  •  佛祖请我去吃肉
    2020-12-03 13:55

    Using next and enumerate:

    next((i for i, c in enumerate(a) if c != ' '), len(a))
    

    For any whitespace:

    next((i for i, c in enumerate(a) if not c.isspace()), len(a))
    

提交回复
热议问题