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

yipeiwu_com5年前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 opencv 使用中的 ValueError: too many values to unpack

最近在OpenCV-Python接口中使用cv2.findContours()函数来查找检测物体的轮廓。 根据网上的 教程,Python OpenCV的轮廓提取函数会返回两个值...

pyqt 实现QlineEdit 输入密码显示成圆点的方法

pyqt 实现QlineEdit 输入密码显示成圆点的方法

使用自带的函数就可以实现: lineEdit.setEchoMode(QLineEdit.Password) import struct from PyQt5.QtWidgets i...

python距离测量的方法

之所以写这个,其实就是希望能对距离有一些概念,当然这个也是很基础的,不过千里之行始于足下嘛,各种路径算法,比如a*什么的都会用到这个 距离测量有三种方式 1、欧式距离,这个是最常用的距离...

给Python初学者的一些编程技巧

交换变量   x = 6 y = 5 x, y = y, x print x >>> 5 print y >>> 6 if...

使用python实现回文数的四种方法小结

回文数就是指整数倒过来和原整数相等。 Example 1: Input: 121 Output: true Example 2: Input: -121 Output:...