美文网首页
Python字符串操作

Python字符串操作

作者: 微凉_半夏 | 来源:发表于2023-04-11 12:07 被阅读0次

isdigit()
在 Python 中,可以使用isdigit()方法来检查字符串是否可以转换为整数。 如果字符串中的所有字符都是数字并且至少有一个字符,则此方法返回 True,否则返回 False

s = "123"
if s.isdigit():
    print("s can be converted to an integer")
else:
    print("s cannot be converted to an integer")

注意: 当s包含小数点时, 此时将返回False


相关文章

网友评论

      本文标题:Python字符串操作

      本文链接:https://www.haomeiwen.com/subject/ejegddtx.html