无涯教程-Python - isnumeric()函数

104 阅读1分钟

Python isnumeric()检查字符串是否仅由数字字符组成。此方法仅存在于Unicode对象上。

注意-若要将字符串定义为unicode,只需在赋值的开始引号前面加上"u"前缀即可。下面是示例。

isnumeric() - 语法

str.isnumeric()

isnumeric() - 返回值

如果字符串中的所有字符都是数字,则此方法返回true,否则返回false。

isnumeric() - 示例

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

#!/usr/bin/python

str=u"this2009";
print str.isnumeric()

str=u"23443434"; print str.isnumeric()

当无涯教程运行上面的程序时,它产生以下输出-

False
True

参考链接

www.learnfk.com/python/stri…