The isdigit() method is a built-in string method in Python that returns True if all characters in the string are digits, and False otherwise. It helps to check if a string contains only numeric characters.
Parameter Values
This function does not accept any parameters.Return Values
The isdigit() method from Python's String Methods returns a bool, either True or False.
How to Use isdigit() in Python
Example 1:
The isdigit() method returns True if all characters in the string are digits. Otherwise, it returns False.
text = '12345'
result = text.isdigit()
print(result) # Output: True
Example 2:
The isdigit() method returns False if the string contains any non-digit characters, even if it also contains digit characters.
text = '12345a'
result = text.isdigit()
print(result) # Output: False
Example 3:
The isdigit() method does not consider negative numbers or floating-point numbers as digits.
text = '-123.45'
result = text.isdigit()
print(result) # Output: False