How to check if a number has same digit in Python
For example:
check(111) --> True
check(22) --> True
check(111) --> True
check(45) --> False
Are you expecting an answer like this? It will return True if all the digits are same else False.
What I am doing in this answer is, I am taking the first digit then I am multiplying the string form of the digit times the length of the string and using an If statement to check whether they match or not.
def check(n):
if str(n) == str(n)[0] * len(str(n)):
return True
else:
return False
print(check(45))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.