Python判断字符串是否为字母或者数字(浮点数)的多种方法


Posted in Python onAugust 03, 2018

str为字符串s为字符串

str.isalnum() 所有字符都是数字或者字母

str.isalpha() 所有字符都是字母

str.isdigit() 所有字符都是数字

str.isspace() 所有字符都是空白字符、t、n、r

检查字符串是数字/浮点数方法

float部分

>> float('Nan')
nan
>> float('Nan')
nan
>> float('nan')
nan
>> float('INF')
inf
>> float('inf')
inf
>> float('-INF')
inf
>> float('-inf')
inf

第一种:最简单

def is_number(str):
  try:
    # 因为使用float有一个例外是'NaN'
    if str=='NaN':
      return False
    float(str)
    return True
  except ValueError:
    return False
float例外示例
 >>> float('NaN')
 nan

使用complex()

def is_number(s):
  try:
    complex(s) # for int, long, float and complex
  except ValueError:
    return False
  return True

综合1

def is_number(s):
  try:
    float(s) # for int, long and float
  except ValueError:
    try:
      complex(s) # for complex
    except ValueError:
      return False
  return True

综合2-还是无法完全识别

def is_number(n):
  is_number = True
  try:
    num = float(n)
    # 检查 "nan" 
    is_number = num == num  # 或者使用 `math.isnan(num)`
  except ValueError:
    is_number = False
  return is_number
>>> is_number('Nan')  
False
>>> is_number('nan') 
False
>>> is_number('123') 
True
>>> is_number('-123') 
True
>>> is_number('-1.12')
True
>>> is_number('abc') 
False
>>> is_number('inf') 
True

第二种:只能判断是整数

使用isnumeric()

# str必须是uniconde模式
>>> str = u"345"
>>> str.isnumeric()True
http://www.tutorialspoint.com/python/string_isnumeric.htm
http://docs.python.org/2/howt...

使用isdigit()

https://docs.python.org/2/lib...
>>> str = "11"
>>> print str.isdigit()
True
>>> str = "3.14"
>>> print str.isdigit()
False
>>> str = "aaa"
>>> print str.isdigit()
False

使用int()

def is_int(str):
  try:
    int(str)
    return True
  except ValueError:
    return False

第三种:使用正则(最安全方法)

import re
def is_number(num):
  pattern = re.compile(r'^[-+]?[-0-9]\d*\.\d*|[-+]?\.?[0-9]\d*$')
  result = pattern.match(num)
  if result:
    return True
  else:
    return False
>>>: is_number('1')
True
>>>: is_number('111')
True
>>>: is_number('11.1')
True
>>>: is_number('-11.1')
True
>>>: is_number('inf')
False
>>>: is_number('-inf')
False

总结

以上所述是小编给大家介绍的Python判断字符串是否为字母或者数字(浮点数)的多种方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Python 相关文章推荐
Python实现从脚本里运行scrapy的方法
Apr 07 Python
Python中文件的读取和写入操作
Apr 27 Python
Python 查找字符在字符串中的位置实例
May 02 Python
python 解决动态的定义变量名,并给其赋值的方法(大数据处理)
Nov 10 Python
django之对FileField字段的upload_to的设定方法
Jul 28 Python
如何基于python实现脚本加密
Dec 28 Python
python调用HEG工具批量处理MODIS数据的方法及注意事项
Feb 18 Python
在python中logger setlevel没有生效的解决
Feb 21 Python
Python如何使用PIL Image制作GIF图片
May 16 Python
Python键鼠操作自动化库PyAutoGUI简介(小结)
May 17 Python
Python使用Matlab命令过程解析
Jun 04 Python
用Python爬取某乎手机APP数据
Jun 15 Python
OpenCV2从摄像头获取帧并写入视频文件的方法
Aug 03 #Python
python中copy()与deepcopy()的区别小结
Aug 03 #Python
Python爬取个人微信朋友信息操作示例
Aug 03 #Python
python opencv人脸检测提取及保存方法
Aug 03 #Python
Python爬虫爬取新浪微博内容示例【基于代理IP】
Aug 03 #Python
OpenCV+python手势识别框架和实例讲解
Aug 03 #Python
Windows下将Python文件打包成.EXE可执行文件的方法
Aug 03 #Python
You might like
PHP Curl出现403错误的解决办法
2014/05/29 PHP
php实现微信原生支付(扫码支付)功能
2018/05/30 PHP
PHP代码覆盖率统计详解
2020/07/22 PHP
js 本地预览的简单实现方法
2014/02/18 Javascript
Javascript的setTimeout()使用闭包特性时需要注意的问题
2014/09/23 Javascript
nodejs实现获取某宝商品分类
2015/05/28 NodeJs
逐一介绍Jquery data()、Jquery stop()、jquery delay()函数(详)
2015/11/04 Javascript
javascript中checkbox使用方法简单实例演示
2015/11/17 Javascript
浅析js绑定事件的常用方法
2016/05/15 Javascript
javascript超过容器后显示省略号效果的方法(兼容一行或者多行)
2016/07/14 Javascript
jQuery 特性操作详解及实例代码
2016/09/29 Javascript
Textarea输入字数限制实例(兼容iOS&安卓)
2017/07/06 Javascript
JavaScript如何对图片进行黑白化
2018/04/10 Javascript
解决axios会发送两次请求,有个OPTIONS请求的问题
2018/10/25 Javascript
linux服务器快速卸载安装node环境(简单上手)
2021/02/22 Javascript
[46:12]完美世界DOTA2联赛循环赛 DM vs Matador BO2第一场 11.04
2020/11/04 DOTA
python实现RSA加密(解密)算法
2016/02/17 Python
使用简单工厂模式来进行Python的设计模式编程
2016/03/01 Python
python实现图书管理系统
2018/03/12 Python
python 列表,数组,矩阵两两转换tolist()的实例
2018/04/04 Python
python selenium 获取接口数据的实现
2020/12/07 Python
纯CSS3实现地球自转实现代码(图文教程附送源码)
2012/12/26 HTML / CSS
分享一个页面平滑滚动小技巧(推荐)
2019/10/23 HTML / CSS
俄罗斯天然和有机产品、健康生活网上商店:Fitomarket.ru
2020/10/09 全球购物
办公室秘书岗位职责范本
2014/02/11 职场文书
毕业生个人求职自荐信
2014/02/26 职场文书
春季防火方案
2014/05/10 职场文书
最新离婚协议书范本
2014/08/19 职场文书
党员个人对照检查材料思想汇报
2014/09/16 职场文书
家庭困难证明
2014/10/12 职场文书
党的群众路线调研报告
2014/11/03 职场文书
数学教师个人总结
2015/02/06 职场文书
小学四年级作文之写景
2019/08/23 职场文书
导游词之南迦巴瓦峰
2019/11/19 职场文书
python用海龟绘图写贪吃蛇游戏
2021/06/18 Python
2021好看的国漫排行榜前十名 《完美世界》上榜,《元龙》排名第一
2022/03/18 国漫