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随机数分布random均匀分布实例

python随机数分布random均匀分布实例

因为概率问题,所以需要测试一下python的随机数分布。到底是平均(均匀)分布,还是正态(高斯)分布。 测试代码如下: #! /usr/bin/env python #coding=...

Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法

Python 出现错误TypeError: ‘NoneType' object is not iterable解决办法 TypeError: 'NoneType' object is n...

python3.6使用urllib完成下载的实例

如下所示: url = u'http://tieba.baidu.com/f?kw=权利的游戏&ie=utf-8&pn=50' url = quote(url, safe =...

Python文件处理

本文给大家介绍Python文件处理相关知识,具体内容如下所示: 1.文件的常见操作 文件是日常编程中常用的操作,通常用于存储数据或应用系统的参数。python提供了os、os.path...

python命令行参数sys.argv使用示例

复制代码 代码如下:#diff.py#!/bin/env python import sys if len(sys.argv) <> 3:   ...