Python isdecimal 字符串函数如果字符串中的所有字符都是十进制字符,则返回 True。否则,它返回 False。在本节中,我们将向您展示如何编写 isdecimal 函数及其示例,语法如下:
String_Value.isdecimal()
Python isdecimal 函数用于检查十进制数
以下示例集可帮助您理解此方法。在第四个语句中,100.23 返回 false,因为 . 符号没有十进制表示。
str1 = "100"
print("Output of str1 = ", str1.isdecimal())
str2 = "Python"
print("Output of str2 = ", str2.isdecimal())
str3 = "100and200"
print("Output of str3 = ", str3.isdecimal())
str4 = "100.23"
print("Output of str4 = ", str4.isdecimal())
str5 = "100 30"
print("Output of str5 = ", str5.isdecimal())

在此 字符串方法 示例中,我们对不同数字的 Unicode 使用了该方法。在最后一个语句中,我们使用了 $ 符号的 Unicode,这就是为什么 Python 返回 false 的原因。
str1 = "\u0033" # Unicode of Digit 3
print("Output of str1 = ", str1.isdecimal())
str2 = "\u0039" # Unicode of Digit 9
print("Output of str2 = ", str2.isdecimal())
str3 = "\u06F1" #Extended Arabic-Indic Digit 1
print("Output of str3 = ", str3.isdecimal())
str4 = "\u07C9" # Niko Digit 9
print("Output of str4 = ", str4.isdecimal())
str5 = "\u096C" # Devanagari Digit 6
print("Output of str5 = ", str5.isdecimal())
str6 = "\u0024" # Unicode of $ Symbol
print("Output of str6 = ", str6.isdecimal())
Output of str1 = True
Output of str2 = True
Output of str3 = True
Output of str4 = True
Output of str5 = True
Output of str6 = False