Sum of all digits in a string
You're right, it can be turned into one line using comprehension syntax which would be your best option of you're looking for a slightly more efficient code.


isdecimal() is better to use in this case than isnumeric() because the isnumeric() accepts types like ½ and ¼ and n² which might produce some side effects you don't want if you're using this function for some real application. Apart from this the code looks good, can be improved using the comprehension syntax which is more efficient and shorter.


Improved version:

def sum_digits(chars: str) -&gt; int:
    """Return sum of numbers in chars"""
    return sum(int(char) for char in chars if char.isdecimal())


if __name__ == '__main__':
    print(sum_digits('abcd173fg'))
    print(sum_digits('abcd'))
    print(sum_digits('173678'))