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