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 Django 实现简单注册功能过程详解

Python Django 实现简单注册功能过程详解

项目创建略,可参考Python Django Vue 项目创建。 目录结构如下 编辑views.py from django.shortcuts import render #...

Python字符串转换成浮点数函数分享

利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456 from functools import reduce def s...

Python 200行代码实现一个滑动验证码过程详解

Python 200行代码实现一个滑动验证码过程详解

前言 做网络爬虫的同学肯定见过各种各样的验证码,比较高级的有滑动、点选等样式,看起来好像挺复杂的,但实际上它们的核心原理还是还是很清晰的,本文章大致说明下这些验证码的原理以及带大家实现一...

浅谈Python中列表生成式和生成器的区别

列表生成式语法: [x*x for x in range(0,10)] //列表生成式,这里是中括号 //结果 [0, 1, 4, 9, 16, 25, 36, 49, 64, 81...

Python 3.8 新功能全解

Python 3.8是Python语言的最新版本,它适合用于编写脚本、自动化以及机器学习和Web开发等各种任务。现在Python 3.8已经进入官方的beta阶段,这个版本带来了许多语法...