Python string Method - isdecimal()
The isdecimal()
method is a built-in Python string method that returns a boolean value indicating whether all the characters in the string are decimal characters (i.e., characters used to represent numbers in base 10).
Here's an example that demonstrates the usage of the isdecimal()
method:
string1 = "123" string2 = "123.45" print(string1.isdecimal()) # Output: True print(string2.isdecimal()) # Output: False
In this example, we create two strings: string1
which contains only decimal characters, and string2
which contains a non-decimal character (a decimal point). We then call the isdecimal()
method on each string. The method returns True
for string1
since all its characters are decimal characters, and False
for string2
since it contains a non-decimal character.
The isdecimal()
method returns True
if the string is not empty and all its characters are decimal characters, and False
otherwise. Here are some additional examples:
print("".isdecimal()) # Output: False print("123ABC".isdecimal()) # Output: False print("123".isdecimal()) # Output: True
In the first example, we call the isdecimal()
method on an empty string, which returns False
since the string is not decimal. In the second example, we call the method on a string containing non-decimal characters, which returns False
. In the third example, we call the method on a string containing Unicode decimal characters (i.e., full-width digits), which returns True
. Note that the isdecimal()
method only recognizes characters in the ASCII range as decimal characters; full-width digits are recognized as decimal characters by isdecimal()
but not by isdigit()
.