python通过自定义isnumber函数判断字符串是否为数字的方法

yipeiwu_com6年前Python基础

本文实例讲述了python通过自定义isnumber函数判断字符串是否为数字的方法。分享给大家供大家参考。具体如下:

''' isnumeric.py
test a numeric string s if it's usable
for int(s) or float(s)
'''
def isnumeric(s):
  '''returns True if string s is numeric'''
  return all(c in "0123456789.+-" for c in s)
# test module ...
if __name__ == '__main__':
  print(isnumeric('123'))   # True
  print(isnumeric('-123.45')) # True
  print(isnumeric('+3.14'))  # True
  print(isnumeric('$99.95'))  # False

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Python导入txt数据到mysql的方法

本文实例讲述了Python导入txt数据到mysql的方法。分享给大家供大家参考。具体分析如下: 从TXT文本转换数据到MYSQL数据库,接触一段时间python了 第一次写东西 用的是...

总结python实现父类调用两种方法的不同

总结python实现父类调用两种方法的不同

python中有两种方法可以调用父类的方法: super(Child, self).method(args)  Parent.method(self, args) 我用其中的一...

python处理按钮消息的实例详解

python处理按钮消息的实例详解

python处理按钮消息的实例详解            最新学习Python的基础知...

django自带serializers序列化返回指定字段的方法

django orm 有个defer方法,指定模型排除的字段。 如下返回的Queryset, 排除‘username', 'id'。 users=models.UserInfo.ob...

使用pandas将numpy中的数组数据保存到csv文件的方法

使用pandas将numpy中的数组数据保存到csv文件的方法

接触pandas之后感觉它的很多功能似乎跟numpy有一定的重复,尤其是各种运算。不过,简单的了解之后发现在数据管理上pandas有着更为丰富的管理方式,其中一个很大的优点就是多出了对数...