isdigit() is an in-built method in Python, which is used to check whether a string contains only digits or not.
isdigit()是Python中的内置方法,用于检查字符串是否仅包含数字。
Digit value contains all decimal characters and other digits which may represent power or anything related to this, but it should be a complete digit, like "3".
数字值包含所有十进制字符和其他数字,这些数字可能表示幂或与此有关的任何东西,但它应该是完整的数字,例如“ 3” 。
This method is different from isdecimal() method and isnumeric() method because first method checks only decimal characters and second method checks numeric values including decimal characters. And this (isdigit()) method will consider only digit.
此方法不同于isdecimal()方法和isnumeric()方法,因为第一个方法仅检查十进制字符,第二个方法检查包括十进制字符的数值。 并且此( isdigit() )方法将仅考虑digit。
Note:
注意:
Digits value contains decimal characters and other Unicode digits, fractional part of the value like ½, ¼ etc are not considered as digit.
数字值包含十进制字符和其他Unicode数字,该值的小数部分(如½,¼等)不视为数字。
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.isdigit();
Parameter: None
参数:无
Return type:
返回类型:
true - If all characters of the string are digits then method returns true.
true-如果字符串的所有字符都是数字,则method返回true 。
false - If any of the characters of the string is not a digit then method returns false.
false-如果字符串中的任何字符都不是数字,则方法返回false 。
Example/program:
示例/程序:
# Only digits (decimal characters)
str1 = u"362436"
print (str1.isdigit())
# digit value (no decimals but a digit)
str2 = u"3"
print (str2.isdigit())
# numeric values but not any digit
str3 = u"½¼"
print (str3.isdigit())
#digits, alphabets
str4 = u"Hello3624"
print (str4.isdigit())
Output
输出量
True
True
True
False
Reference: String isdigit()
参考: 字符串isdigit()
翻译自: https://www.includehelp.com/python/string-isdigit-method-with-example.aspx