python编写函数判断字符串是否为纯数字

编程入门 行业动态 更新时间:2024-10-06 12:33:58

python编写函数判断<a href=https://www.elefans.com/category/jswz/34/1771434.html style=字符串是否为纯数字"/>

python编写函数判断字符串是否为纯数字

/

以下实例通过创建自定义函数 is_number() 方法来判断字符串是否为数字:

# -*- coding: UTF-8 -*-

# Filename : test.py

# author by : www.w3cschool

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('1')) # True

print(is_number('1.3')) # True

print(is_number('-1.37')) # True

print(is_number('1e3')) # True

# 测试 Unicode

# 阿拉伯语 5

print(is_number('٥')) # True

# 泰语 2

print(is_number('๒')) # True

# 中文数字

print(is_number('四'))

更多推荐

python编写函数判断字符串是否为纯数字

本文发布于:2024-02-07 07:01:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   函数   数字   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!