The isdigit function in Python provides a True output when every character within the string constitutes a digit. Conversely, it yields a False result if there are no digit characters present in the string.
Python String isdigit Method Syntax
It has the following syntax:
Example
isdigit()
Parameters
No parameter is required.
Return
It returns either True or False.
Different Examples for Python String isdigit Method
Let’s explore a few examples of the isdigit method to gain a better understanding of its capabilities.
Example 1
An elementary illustration of digit verification utilizing the isdigit function.
Example
# Python isdigit() method example
# Variable declaration
str = '12345'
# Calling function
str2 = str.isdigit()
# Displaying result
print(str2)
Output:
Example 2
It yields a true value exclusively when every character is a digit. Refer to the example provided below.
Example
# Python isdigit() method example
# Variable declaration
str = "12345"
str3 = "120-2569-854"
# Calling function
str2 = str.isdigit()
str4 = str3.isdigit()
# Displaying result
print(str2)
print(str4)
Output:
Output
True
False
Example 3
In programming, we can utilize this to determine if a string includes any numeric characters.
Example
# Python isdigit() method example
# Variable declaration
str = "123!@#$"
if str.isdigit() == True:
print("String is digit")
else:
print("String is not digit")
Output:
Output
String is not digit