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中DataFrame按照行遍历的方法

在做分类模型时候,需要在DataFrame中按照行获取数据以便于进行训练和测试。 import pandas as pd dict=[[1,2,3,4,5,6],[2,3,4,5,6...

Python3.6实现带有简单界面的有道翻译小程序

本人使用的是Python3.6(32bit),在win10上运行的     代码如下: from tkinter import * import url...

python中sys.argv函数精简概括

python中sys.argv函数精简概括

sys.argv函数通常用来读取命令行参数,其中保存了程序的文件名和命令行参数,读入的参数以元组的形式保存。下面以sys.argv[0],sys.argv[1],sys.argv[1:]...

python获取当前运行函数名称的方法实例代码

python获取当前运行函数名称的方法实例代码 摘要: c/c++中获取函数所在源码名,函数名和行号的方法很简单 __FILE__,__FUNCTION__和__LINE__ pytho...

python版本坑:md5例子(python2与python3中md5区别)

起步 对于一些字符,python2和python3的md5加密出来是不一样的. # python2.7 pwd = "xxx" + chr(163) + "fj" checkcode...