Find the leftmost digit that occurs in a given string.
Example
-
- For
inputString = "var_1__Int"
, the output should befirstDigit(inputString) = '1'
; - For
inputString = "q2q-q"
, the output should befirstDigit(inputString) = '2'
; - For
inputString = "0ss"
, the output should befirstDigit(inputString) = '0'
.
- For
我的解答:
def firstDigit(inputString): return [i for i in inputString if i.isdigit()][0]
def firstDigit(inputString): for i in inputString: if i.isdigit(): return i