今日问答
简介
大家好,我是麦克阿洛。如果你喜欢我的文章,欢迎订阅
和关注
哦。
视频教程
操作步骤
-
描述
Python isdigit() 方法检测字符串是否只由数字组成,只对 0 和 正数有效。 -
语法
isdigit()方法语法:
str.isdigit()
-
返回值
如果字符串只包含数字则返回 True 否则返回 False。 -
编写代码
# !/usr/bin/python
# -*- coding: UTF-8 -*-
# 字符串只包含数字
strTest = "123456789"
print(strTest.isdigit()) # 打印结果: True
strTest = "都是字符串的测试代码"
print(strTest.isdigit()) # 打印结果: False
strTest = None
# print(strTest.isdigit()) # 打印结果: AttributeError: 'NoneType' object has no attribute 'isdigit'
strTest = "0"
print(strTest.isdigit()) # 打印结果: True
strTest = "-1"
print(strTest.isdigit()) # 打印结果:False
5. 输出结果:
参考资料
无
结束の语
今天的教程到这里就结束了。如果想获得更多的内容,请关注
我哦,谢谢大家。