isnumeric
isnumeric() is an in-built method in Python, which is used to check whether a string contains only numeric values or not.
isnumeric()是Python中的内置方法,用于检查字符串是否仅包含数字值。
Numeric contain all decimal characters and the fraction type values like (Half (½), one fourth (¼)). This method is different from isdecimal() Method because it can check other numeric values except only decimal characters (from 0 to 10).
数值包含所有十进制字符和分数类型值,例如(半(1/2),四分之一(¼))。 此方法不同于isdecimal()方法,因为它可以检查除十进制字符(0到10)之外的其他数字值。
Note:
注意:
Numeric values contains decimal characters (all digits from 0 to 9) and fractional part of the values like ½, ¼.
数值包含十进制字符(从0到9的所有数字)和值的小数部分,例如½,¼ 。
String should be Unicode object - to define a string as Unicode object, we use u as prefix of the string value.
字符串应为Unicode对象-要将字符串定义为Unicode对象,我们使用u作为字符串值的前缀。
Syntax:
句法:
String.isnumeric();
Parameter: None
参数:无
Return type:
返回类型:
true - If all characters of the string are numeric then method returns true.
true-如果字符串的所有字符均为数字,则方法返回true 。
false - If any of the characters of the string is not a numeric then method returns false.
false-如果字符串中的任何字符都不是数字,则方法返回false 。
Example/program:
示例/程序:
# numeric values (decimal characters)
str1 = u"362436"
print str1.isnumeric()
# numeric values (no decimal)
str2 = u"½¼"
print str2.isnumeric()
# nemeric values (with decimals)
str3 = u"½¼3624"
print str3.isnumeric()
# numeric values with alphabets
str4 = u"Hello½¼3624"
print str4.isnumeric()
Output
输出量
TrueTrueTrueFalse
Reference: String isnumeric()
参考: 字符串isnumeric()
翻译自: https://www.includehelp.com/python/string-isnumeric-method-with-example.aspx
isnumeric