Python字串isnumeric()方法

Python字串isnumeric()方法檢查字串是否僅包含數字字元。此方法僅用於unicode對象上。

注意 - 與Python 2不同,所有字串在Python 3中都用Unicode表示。下麵給出了一個示例。

語法

以下是isnumeric()方法的語法 -

str.isnumeric()

參數

  • NA

返回值

  • 如果字串中的所有字元都是數字,則此方法返回true,否則返回false

示例

以下示例顯示了isnumeric()方法的用法。

#!/usr/bin/python3


str = "this2018"
print (str.isnumeric())

str = "121323"
print (str.isnumeric())

當運行上面的程式,它產生以下結果 -

False
True

上一篇: Python字串 下一篇: Python列表