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