Python判断字符串是否为数字(数字、小数、负数、负小数、0)

支持多种判断条件,一步到位。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# author:zhoulong_GISER
# blog:https://blog.csdn.net/qq_33356563
#判断字符串是否是数字(数字、小数、负数、负小数、0)
#字符串
str_numbers = ["-0.3","0","2","0.002","-5","china","中国","-like","-中国"]
for str_number in str_numbers:
    if (str_number.split(".")[0]).isdigit() or str_number.isdigit() or  (str_number.split('-')[-1]).split(".")[-1].isdigit():
        print(str_number+"是数字")
    else:
        print(str_number+"不是数字")

猜你喜欢

转载自blog.csdn.net/qq_33356563/article/details/85255016