Count consecutive spaces(&nbsp) at the start of the string in Python [closed]

The most pythonic way to do this is the following:

def count_start_spaces(s):
    return len(s) - len(s.lstrip())

Given the following input:

strings = ["Test1 False (String 1)","    Test2 False (String 2)","        Test3 False (String 3)"]
list(map(count_start_space, strings))
# output: [0, 4, 8]

Leave a Comment