python判断一个字符串是否为小数

def is_float(s):
    s=str(s)
    if s.count('.')==1: #判断小数点个数
        left,right = s.split('.') #按照小数点进行分割
        if left.startswith('-') and left.count('-')==1 and right.isdigit():
            lleft = left.split('-')[1]
            if lleft.isdigit():
                return True
        elif left.isdigit() and right.isdigit():
            return True
    return False

print(is_float('ss'))
print(is_float(.1))

 

posted @ 2024-04-23 11:33  cindylpp  阅读(154)  评论(0编辑  收藏  举报