Python3 中的 isnumeric()函数

首页 / Python3入门教程 / Python3 中的 isnumeric()函数

Python isnumeric()方法检查字符串的所有字符是否都是数字字符。如果所有字符都为True,则返回True,否则返回False。

数字字符包括数字字符以及所有具有Unicode数值属性的字符。

isnumeric - 语法

isnumeric()

isnumeric - 返回

它返回True或False。

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/python3/python-string-isnumeric-method.html

来源:LearnFk无涯教程网

在这里,创建了一个简单的示例来检查字符串是否为数字。

# Python isnumeric() method example
# 变量声明
str = "12345"
# 函数调用
str2 = str.isnumeric()
# 显示结果
print(str2)

输出

True

让无涯教程在非数字字符串上对其进行测试,看看它是否返回False。

# Python isnumeric() method example
# 变量声明
str = "javatpoint12345"
# 函数调用
str2 = str.isnumeric()
# 显示结果
print(str2)

输出

False

查看在python编程中如何以及如何应用isnumeric()方法的场景

# Python isnumeric() method example
str = "123452500" # True
if str.isnumeric() == True:
    print("Numeric")
else:
    print("Not numeric")
str2 = "123-4525-00" # False
if str2.isnumeric() == True:
    print("Numeric")
else:
    print("Not numeric")

输出

Numeric
Not numeric

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Go语言核心36讲 -〔郝林〕

代码精进之路 -〔范学雷〕

Python核心技术与实战 -〔景霄〕

编辑训练营 -〔总编室〕

浏览器工作原理与实践 -〔李兵〕

人人都用得上的写作课 -〔涵柏〕

Tony Bai · Go语言第一课 -〔Tony Bai〕

PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕

AI 应用实战课 -〔黄佳〕

好记忆不如烂笔头。留下您的足迹吧 :)