import re
lis = ['06', '+', '(', '0.0.9', '÷', '2', '+', '3.2', ')', '*', '5','4.30','0.4','007.8']
# 判断字符串是否是小数或者整数
def isDecimalOrInt(s):
#去除字符串两边的空格
s= s.strip()
flag = True
#判断是否是整数
intRet=re.match("^[0-9]+$",s)
#判断是否是小数
decRet= re.match("^\d+\.\d+$",s)
#如果是整数
if intRet:
if len(s)>1 and s[0]=="0":
#如果整数的长度大于1,那么判断其首位是否为0,如果为零返回false
flag = False
return flag
elif decRet:
pos = s.index(".")
if pos!=1 and s[0]=="0":
flag = False
return flag
else:
flag = False
return flag
return flag
for i in lis:
flag=isDecimalOrInt(i)
print(i+":"+str(flag))
Python 小练习-1 判断字符串是否为小数或者整数
最新推荐文章于 2024-11-06 20:36:59 发布