isdigit()
在 Python 中,可以使用isdigit()
方法來檢查字符串是否可以轉換為整數(shù)。 如果字符串中的所有字符都是數(shù)字并且至少有一個字符,則此方法返回 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
包含小數(shù)點時, 此時將返回False