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面向对象程序设计OOP深入分析【构造函数,组合类,工具类等】

本文深入分析了Python面向对象程序设计OOP。分享给大家供大家参考,具体如下: 下面是一个关于OOP的实例,模块文件为person.py # File person.py(sta...

tensorflow estimator 使用hook实现finetune方式

为了实现finetune有如下两种解决方案: model_fn里面定义好模型之后直接赋值 def model_fn(features, labels, mode, params):...

Python中多个数组行合并及列合并的方法总结

采用numpy快速将两个矩阵或数组合并成一个数组: import numpy as np 数组 a = [[1,2,3],[4,5,6]] b = [[1,1,1],[2,2,...

Python适配器模式代码实现解析

Python适配器模式,代码,思考等 # -*- coding: utf-8 -*- # author:baoshan class Computer: def __init__(...

Django用户认证系统 User对象解析

User对象 User对象是认证系统的核心。用户对象通常用来代表网站的用户,并支持例如访问控制、注册用户、关联创建者和内容等。在Django认证框架中只有一个用户类,例如超级用户('s...