I need to check if some variable is an integer within quotes. So, naturally, I did the following:
! pip install smartprint
def isIntStr(n):
# first, the item must be a string
if not isinstance(n, str):
return False
# second, we try to convert it to integer
try:
int(n)
return True
except:
return False
from smartprint import smartprint as sprint
sprint (isIntStr("123_dummy"))
sprint (isIntStr("123"))
sprint (isIntStr(123))
It works as expected with the following output:
isIntStr("123_dummy") : False
isIntStr("123") : True
isIntStr(123) : False
Is there a cleaner way to do this check?