Skip to content

First Digit

Find the leftmost digit that occurs in a given string.

Example

  • For inputString = "var_1__Int", the output should be
    first_digit(inputString) = '1'
  • For inputString = "q2q-q", the output should be
    first_digit(inputString) = '2'
  • For inputString = "0ss", the output should be
    first_digit(inputString) = '0'

Input/Output

  • [input] string inputString

    A string containing at least one digit.

Solution

py
def first_digit(input_string):
    for i in input_string:
        if i.isdigit():
            return i
    return 0


print(first_digit('Abc1d2'))

my thoughts are neither my employer's nor my wife's