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 pip安装包出现:Failed building wheel for xxx错误的解决

出现原因:缺失相应的whl文件。 解决办法:下载并安装对应的whl文件。 提供一个whl文件的下载网址:http://www.lfd.uci.edu/~gohlke/pythonlibs...

python 利用turtle模块画出没有角的方格

python 利用turtle模块画出没有角的方格

意思就是画四条直线,四条直线都不能相交即可。 #!/usr/bin/python #coding: UTF-8 import turtle import time t = t...

基于使用paramiko执行远程linux主机命令(详解)

paramiko是python的SSH库,可用来连接远程linux主机,然后执行linux命令或者通过SFTP传输文件。 关于使用paramiko执行远程主机命令可以找到很多参考资料了,...

浅谈Django中的数据库模型类-models.py(一对一的关系)

如下所示: # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import...

将python图片转为二进制文本的实例

将python图片转为二进制文本的实例

写在最前面: 我在研究机器学习的过程中,给的数据集是手写数字图片被处理后的由0,1表达的txt文件,今天写一写关于图片转化为二进制txt文件的python实践 在这里,我们使用pytho...