如果字符串中的所有字符都是数字,Python的
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
无涯教程可以在编程中使用它来检查字符串是否包含数字。
链接:https://www.learnfk.comhttps://www.learnfk.com/python3/python-string-isdigit-method.html
来源:LearnFk无涯教程网
# Python isdigit() method example # 变量声明 str = "123!@#$" if str.isdigit() == True: print("String is digit") else: print("String is not digit")
输出:
String is not digit
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)