How to check if an integer include specific digits x times in Python

1 Answer

0 votes
def has_digit_x_times(number, digit, xtimes):
    # Convert the number and digit to strings
    number_str = str(number)
    digit_str = str(digit)
    
    # Count the occurrences of the digit in the number
    count = number_str.count(digit_str)
    
    # Check if the count matches the desired number of times
    return count == xtimes

number = 7097175
digit = 7
xtimes = 3

result = has_digit_x_times(number, digit, xtimes)

print(result) 



'''
run:

True

'''

 



answered Apr 26 by avibootz
...