I would recommend adding a Boolean check for the value of the number. I convert a large value in milliseconds to a date and time. I have numbers from 2 to 200, 000, 200, so 0 is the correct conclusion. The function @Chris Mueller has will return 0, even if the number is less than 10 ** n.
def get_digit(number, n): return number // 10**n % 10 get_digit(4231, 5)
def get_digit(number, n): if number - 10**n < 0: return False return number // 10**n % 10 get_digit(4321, 5)
You must be careful when checking the logical state of this return value. To allow 0 as a valid return value, you cannot just use if get_digit: you must use if get_digit is False: so that 0 acts as a false value.
source share