We could also do something like:
def sum_numbers(string: str) -> int:
"""Return sum of numbers in string"""
return sum([string.count(str(i)) * i for i in range(10)])
if __name__ == '__main__':
print(sum_numbers('hel3l4o55')) #should print 17
print(sum_numbers('xdman')) #should print 0
print(sum_numbers('123456789')) #should print 45
This differs from your implementation due to the fact that my loop iterates over the digits while yours loops through the string itself. This means a few things:
If a string has at least one of every digit, this implementation skips out on checking for 0s.
Depending on how Python internally runs the
list.count function, there is a chance this implementation is wildly inefficient overall. I likely loop through the strings more times than its worth, so this becomes worse as the length of the string becomes sizeable enough.
Ultimately, this is just a different idea on what we can loop around. As the number of things we need to search for (and in turn, the number of things we have to run a count on increases), the more times we search through the string, wasting time. However, if we are only looking for one thing or a small sample of possible results, list.count is a good option to have available.
EDIT: might also depend on how floats are looked at as mentioned in the above comments. If they are supposed to just be read as individual chars then this works fine. If you're expected to parse whole sections to determine what is and isn't a float/multi-char number, this falls apart.