How do I check if a string is a number (float)?

后端 未结 30 3930
暗喜
暗喜 2020-11-21 05:16

What is the best possible way to check if a string can be represented as a number in Python?

The function I currently have right now is:

def is_numb         


        
30条回答
  •  暖寄归人
    2020-11-21 05:45

    This code handles the exponents, floats, and integers, wihtout using regex.

    return True if str1.lstrip('-').replace('.','',1).isdigit() or float(str1) else False
    

提交回复
热议问题