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基于分水岭算法解决走迷宫游戏示例

本文实例讲述了Python基于分水岭算法解决走迷宫游戏。分享给大家供大家参考,具体如下: #Solving maze with morphological transformatio...

Python内置的字符串处理函数整理

str='python String function' 生成字符串变量str='python String function'字符串长度获取:len(str)例:print '%s l...

Python算法应用实战之队列详解

Python算法应用实战之队列详解

队列(queue) 队列是先进先出(FIFO, First-In-First-Out)的线性表,在具体应用中通常用链表或者数组来实现,队列只允许在后端(称为rear)进行插入操作,在前端...

python下的opencv画矩形和文字注释的实现方法

画矩形 函数调用:cv2.rectangle(img,pt1,pt2,color,thickness,line_type,shift) img: 图像. pt1: 矩形的一个顶点。 pt...

Python实现八大排序算法

如何用Python实现八大排序算法 1、插入排序 描述 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,时间...