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


Posted in Python onApril 23, 2015

本文实例讲述了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数组遍历的简单实现方法小结
Apr 27 Python
pandas进行数据的交集与并集方式的数据合并方法
Jun 27 Python
pygame游戏之旅 创建游戏窗口界面
Nov 20 Python
Python中三元表达式的几种写法介绍
Mar 04 Python
Django给admin添加Action的步骤详解
May 01 Python
python3 批量获取对应端口服务的实例
Jul 25 Python
python用requests实现http请求代码实例
Oct 31 Python
解决pycharm中导入自己写的.py函数出错问题
Feb 12 Python
JupyterNotebook 输出窗口的显示效果调整方法
Apr 13 Python
用pip给python安装matplotlib库的详细教程
Feb 24 Python
tensorflow学习笔记之tfrecord文件的生成与读取
Mar 31 Python
pytorch实现ResNet结构的实例代码
May 17 Python
用Python给文本创立向量空间模型的教程
Apr 23 #Python
用Python进行行为驱动开发的入门教程
Apr 23 #Python
python正常时间和unix时间戳相互转换的方法
Apr 23 #Python
python执行等待程序直到第二天零点的方法
Apr 23 #Python
在Python中测试访问同一数据的竞争条件的方法
Apr 23 #Python
python实现在每个独立进程中运行一个函数的方法
Apr 23 #Python
python输出指定月份日历的方法
Apr 23 #Python
You might like
Content-type 的说明
2006/10/09 PHP
PHP调用三种数据库的方法(1)
2006/10/09 PHP
php中实现精确设置session过期时间的方法
2014/07/17 PHP
smarty简单分页的实现方法
2014/10/27 PHP
常见php数据文件缓存类汇总
2014/12/05 PHP
ThinkPHP实现递归无级分类――代码少
2015/07/29 PHP
PHP常见数组函数用法小结
2016/03/21 PHP
laravel中短信发送验证码的实现方法
2018/04/25 PHP
js判断浏览器的比较全的代码
2007/02/13 Javascript
指定位置如果有图片显示图片,无图片显示广告的JS
2010/06/05 Javascript
使用jquery获取网页中图片高度的两种方法
2013/09/26 Javascript
js实现字符串的16进制编码不加密
2014/04/25 Javascript
JS小游戏之极速快跑源码详解
2014/09/25 Javascript
去除字符串左右两边的空格(实现代码)
2016/05/12 Javascript
Javascript中常用的检测方法小结
2016/10/08 Javascript
select下拉框插件jquery.editable-select详解
2017/01/22 Javascript
NodeJS父进程与子进程资源共享原理与实现方法
2018/03/16 NodeJs
微信小程序实现简单跑马灯效果
2020/05/26 Javascript
AutoJs实现刷宝短视频的思路详解
2020/05/22 Javascript
Python的IDEL增加清屏功能实例
2017/06/19 Python
python:pandas合并csv文件的方法(图书数据集成)
2018/04/12 Python
python 基于dlib库的人脸检测的实现
2019/11/08 Python
Python装饰器用法与知识点小结
2020/03/09 Python
python查看矩阵的行列号以及维数方式
2020/05/22 Python
python合并多个excel文件的示例
2020/09/23 Python
python从ftp获取文件并下载到本地
2020/12/05 Python
吃透移动端 Html5 响应式布局
2019/12/16 HTML / CSS
Jack Rogers官网:美国经典的女性鞋靴品牌
2019/09/04 全球购物
顶岗实习接收函
2014/01/09 职场文书
教师网络培训感言
2014/03/09 职场文书
网络编辑求职信
2014/04/30 职场文书
人民调解员先进事迹材料
2014/05/08 职场文书
助残日活动总结
2014/08/27 职场文书
2015医院个人工作总结范文
2015/05/21 职场文书
纯html+css实现打字效果
2021/08/02 HTML / CSS
面试官问我Mysql的存储引擎了解多少
2022/08/05 MySQL