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列表