isnumeric() 方法檢查字串是否只包含數字字元。這種方法目前只在Unicode對象中使用。
注:不像Python 2,在Python3所有的字串都是Unicode格式,請看下麵的例子。
語法
以下是 isnumeric() 方法的語法-
str.isnumeric()
參數
-
NA
返回值
如果字串中的所有字元是數字則該方法返回 true,否則返回 false。
示例
下麵的示例顯示 isnumeric()方法的使用。
#!/usr/bin/python3 str = "this2016" print (str.isnumeric()) str = "23443434" print (str.isnumeric())
當我們運行上面的程式,會產生以下結果 -
False True