python - How can I check if a string represents an int, without using try/except? - Stack Overflow

PHOTO EMBED

Mon Jul 25 2022 14:27:57 GMT+0000 (Coordinated Universal Time)

Saved by @jacopo #python

def RepresentsInt(s):
    try: 
        int(s)
        return True
    except ValueError:
        return False

>>> print RepresentsInt("+123")
True
>>> print RepresentsInt("10.0")
False
content_copyCOPY

https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except