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

前端 未结 6 1744
-上瘾入骨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:49

    i recently had a similar task of counting indents, because of which i wanted to count tab as four spaces:

    def indent(string: str):
    return sum(4 if char is '\t' else 1 for char in string[:-len(string.lstrip())])
    

提交回复
热议问题