Is there some function like str.isnumeric but applicable to float?
'13.37'.isnumeric() #False
I still use this:
def isFloat(string):
try:
float(string)
return True
except ValueError:
return False
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As Imran says, your code is absolutely fine as shown.
However, it does encourage clients of
isFloatdown the “Look Before You Leap” path instead of the more Pythonic “Easier to Ask Forgiveness than Permission” path.It’s more Pythonic for clients to assume they have a string representing a float but be ready to handle the exception that will be thrown if it isn’t.
This approach also has the nice side-effect of converting the string to a float once instead of twice.