Python 判断字符串是否为数字
转载: http://www.runoob.com/python3/python3-check-is-number.html
以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为数字:
# -*- coding: UTF-8 -*- # Filename : test.py
# author by : www.runoob.com def is_number(s):
try:
float(s)
return True
except ValueError:
pass try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass return False # 测试字符串和数字
print(is_number('foo')) # False
print(is_number('')) # True
print(is_number('1.3')) # True
print(is_number('-1.37')) # True
print(is_number('1e3')) # True # 测试 Unicode
# 阿拉伯语 5
print(is_number('٥')) # False
# 泰语 2
print(is_number('๒')) # False
# 中文数字
print(is_number('四')) # False
# 版权号
print(is_number('©')) # False
执行以上代码输出结果为:
False
True
True
True
True
False
False
False
False
Python 判断字符串是否为数字的更多相关文章
- python判断字符串是字母 数字 大小写
字符串.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False. 字符串.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False. 字符串.is ...
- python 判断字符串是字母 数字 大小写还是空格
str.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False. str.isalpha() 所有字符都是字母(当字符串为中文时, 也返回True),为真返回 T ...
- python 判断字符串是否以数字结尾
import re def end_num(string): #以一个数字结尾字符串 text = re.compile(r".*[0-9]$") if text.match(st ...
- Python判断字符串是否为字母或者数字
严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...
- Python中判断字符串是否为数字的三个方法isdecimal 、isdigit、isnumeric的差别
isdecimal .isdigit.isnumeric这三个字符串方法都用于判断字符串是否为数字,为什么用三个方法呢?他们的差别是什么内? isdecimal:是否为十进制数字符,包括Unicode ...
- python判断字符串
python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...
- C#判断字符串是否是数字
/// <summary> /// 判断字符串是否是数字 /// </summary> public static bool IsNumber(string s) { if ( ...
- 字符串--java中判断字符串是否为数字的方法的几种方法?
ava中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...
- shell 判断字符串是否为数字
本篇文章主要介绍了"shell 判断字符串是否为数字",主要涉及到shell 判断字符串是否为数字方面的内容,对于shell 判断字符串是否为数字感兴趣的同学可以参考一下. #!/ ...
随机推荐
- posgresql
ubuntu下 修改postgres数据库用户的密码为123456 sudo -u postgres psql postgres=# ALTER USER postgres WITH PASSWORD ...
- 配置LVS + Keepalived高可用负载均衡集群之图文教程
负载均衡系统可以选用LVS方案,而为避免Director Server单点故障引起系统崩溃,我们可以选用LVS+Keepalived组合保证高可用性. 重点:每个节点时间都同步哈! C++代码 [r ...
- 入门:Java Map<String,String>遍历及修改
重点:在使用Map时注意key-value,key用于检索value的内容. 在正常情况下,可以不允许重复:在java中分为2中情况,一是内存地址重复,另一个是不同的地址但内容相等. 在使用Map是一 ...
- Web安全之SQL注入攻击技巧与防范
http://www.plhwin.com/2014/06/13/web-security-sql/
- TeXstudio 编写Latex论文的若干问题
TeXstudio 编写Latex论文的若干问题解决方案总结 问题1: 如何安装TeXstudio 以及 Texstudio当中的中文字体使用问题. 一.如何安装TeXstudio 很 ...
- 关于JSF中immediate属性的总结(三)
Okay, when should I use the immediate attribute? If it isn't entirely clear yet, here's a summary, c ...
- 【jQuery】初始化的三种方法
JQ初始化方法实际上有两种,由于美元符号可以定义 jQuery,那么就有三种方法可以进行初始化操作,根据个人习惯来选择吧! 第一种 $(document).ready(function(){ // j ...
- 2.2、Hibernate用注解方式实现一对多、多对多关系
一.一对多关系 1.在上一篇日志中用.xml配置文件项目基础上,再往lib目录先添加一个包-hibernate-jpa-2.0-api-1.0.0.Final.jar 2.新建一个com.st.bea ...
- Spring-test使用JUnit时,测试类autowired报错,create bean error
Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...
- __run_timers() -- 处理全部超时定时器
__run_timers() -- 处理全部超时定时器 run_timer_softirq() --> __run_timers() /usr/src/linux-/kernel/timer.c ...