What is isdecimal() in Python?

The isdecimal() function in Python tells whether a string contains all numeric characters.

Syntax

The syntax of the isdecimal() method is as follows:

string.isdecimal()

Parameters

The isdecimal() method does not take any parameters.

Return value

isdecimal() returns a boolean value. If the string contains all decimal characters, the function returns True. Otherwise, it returns False.

Code

In the code below, the isdecimal() function returns False when the string contains either all alphabet or alphanumeric characters. However, in the case of a complete numeric string, the method returns True.

# contains all numbers
num = "12345"
print(num.isdecimal())
# contains all alphabets
alpha = "abcdef"
print(alpha.isdecimal())
# contains both alphabets and numbers
alphanum = "abc34de78"
print(alphanum.isdecimal())

Free Resources