May-23-2022, 08:39 PM
The str.isnumeric() function is lacking when it comes to detecting floats.
Several sites say to use a try/except to attempt the conversion. I was taught not to depend on try except in the regular program flow, and that it should only be used in the case of real exceptions, like catching runtime errors.
To that end, I made the function below.
Is there a better way?
Would you change anything about this?
Several sites say to use a try/except to attempt the conversion. I was taught not to depend on try except in the regular program flow, and that it should only be used in the case of real exceptions, like catching runtime errors.
To that end, I made the function below.
Is there a better way?
Would you change anything about this?
def is_number(string): if string.isnumeric(): return True if string.count('.') == 1: if string.replace('.', '').isnumeric(): return True return FalseThanks for looking.