Python字串isdigit()
方法檢查字串是否僅由數字組成。
語法
以下是isdigit()
方法的語法 -
str.isdigit()
參數
- NA
返回值
- 如果字串中的所有字元都是數字,並且至少有一個字元,則此方法返回
true
,否則返回false
。
示例
以下示例顯示了isdigit()
方法的用法。
#!/usr/bin/python3
str = "1231234"; # Only digit in this string
print (str.isdigit())
str = "this is string example....wow!!!"
print (str.isdigit())
當運行上面的程式,它產生以下結果 -
True
False