Python3 中的 isdigit()函数

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

如果字符串中的所有字符都是数字,Python的 isdigit()方法将返回True。如果字符串中没有数字,则返回False。

isdigit - 语法

isdigit()

isdigit - 返回

它返回True或False。

无涯教程网

使用isdigit()方法进行数字测试的简单示例。

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

输出

True

仅当所有字符均为数字时,它才返回true。请参见下面的示例。

# Python isdigit() method example
# 变量声明
str = "12345"
str3 = "120-2569-854"
# 函数调用
str2 = str.isdigit()
str4 = str3.isdigit()
# 显示结果
print(str2)
print(str4)

输出

True
False

无涯教程可以在编程中使用它来检查字符串是否包含数字。

# Python isdigit() method example
# 变量声明
str = "123!@#$"
if str.isdigit() == True:
    print("String is digit")
else:
    print("String is not digit")

输出

String is not digit

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

技术教程推荐

技术与商业案例解读 -〔徐飞〕

程序员进阶攻略 -〔胡峰〕

iOS开发高手课 -〔戴铭〕

Redis核心技术与实战 -〔蒋德钧〕

容器实战高手课 -〔李程远〕

编程高手必学的内存知识 -〔海纳〕

计算机基础实战课 -〔彭东〕

Serverless进阶实战课 -〔静远〕

徐昊 · AI 时代的软件工程 -〔徐昊〕

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