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

前端 未结 6 1756
-上瘾入骨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 14:00

    That looks... great to me. Usually I answer "Is X Pythonic?" questions with some functional magic, but I don't feel that approach is appropriate for string manipulation.

    If there were a built-in to only return the leading spaces, and the take the len() of that, I'd say go for it- but AFAIK there isn't, and re and other solutions are absolutely overkill.

提交回复
热议问题