Most text questions are solved by walking the characters once while carrying a small state: "am I inside a word right now?", "how long is the current run?". At every character the state is updated and, where needed, a counter goes up.
digits = 0inside = Falsefor ch in text:if ch.isdigit():if not inside:digits += 1inside = Trueelse:inside = Falsereturn digits
"a12b7" holds two): the counter goes up only at the moment a number is entered, that is, when a digit is seen and the character before was not one. inside remembers what the previous character was.The longest run of the same character is the same skeleton: if the character equals the one before, lengthen the run; if not, start again at 1; update the longest at every step. It is the "compare neighbours" lesson of Arrays, in text.