Python String isdecimal()
Python String isdecimal() method returns True if each of the character in given string is only a decimal (0-9).
If the string contains one or more characters that does not belong to decimal group, then isdecimal() returns False.
In this tutorial, we will learn the syntax and examples for isdecimal() method of String class.
Syntax
The syntax of String isdecimal() method in Python is
str.isdecimal()
Example
In this example, we will take a string '53241'
, and check if this string is decimal using str.isdecimal()
method.
Python Program
x = '53241'
result = x.isdecimal()
print(result)
Output
True
String contains non-decimal character
In this example, we will take a string 'a12'
, and check if this string is decimal using str.isdecimal()
method. Since the string contains non-alphabet character a
, isdecimal() returns False.
Python Program
x = 'a12'
result = x.isdecimal()
print(result)
Output
False
Conclusion
In this Python Tutorial, we learned how to check if given string contains only decimal digits, using String method – isdecimal().