Python String isdigit() method
Python String isdigit() method
The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false.
Syntax
string.isdigit()
Parameter
NA
Return
This method returns a Boolean value true if all characters in the string are digits else for even one non-digit character it returns False.
Example 1
# Python program explaining # the isdigit() method # initializing the string value strVal = "50800" # checking whether the passed string is a digit or not result = strVal.isdigit() # printing the isdigit() result print("The isdigit() method returns: ",result)
Output
The isdigit() method returns: True
Example 2
# Python program explaining # the isdigit() method # initialising the string value strVal = "M0n1caG3ll3r" print("String 1:",strVal) # validating if and else condition if strVal.isdigit() == True: print("All characters of specified string are digits.") else: print("All the characters are not digits.") # passing another string strVal1="123234675" print("String 2:",strVal1) # again validating if and else condition if strVal1.isdigit() == True: print("All characters of specified string are digits.") else: print("All the characters are not digits.")
Output
String 1: M0n1caG3ll3r All the characters are not digits. String 2: 123234675 All characters of specified string are digits.
Example 3
# Python program explaining # the isdigit() method strVal1 = '23455' print(strVal1.isdigit()) #s = '²3455' # subscript is a digit strVal2 = '\u00A223455' print(strVal2.isdigit()) # s = '½' # fraction is not a digit strVal3 = '\u00BD' print(strVal3.isdigit()) #passing special characters in the string strVal4 = '!@9876/0' print(strVal4.isdigit())
Output
True False False False