The Python String isdecimal() method is a built-in function that returns true if all the characters in a string are decimal. If one of the characters is not decimal in the string, it returns false.
Syntax
The Syntax of isdecimal()
method is:
string.isdecimal()
Parameters
The isdecimal()
method does not take any parameters.
Return Value
The isdecimal()
method returns
True
if all the characters in a string are valid decimal characters.False
if one or more characters in a string are not decimal characters.
Example 1: Working of isdecimal() method in Python
# Python3 program to demonstrate the use
# of isdecimal()
s = "12345"
print(s.isdecimal())
# contains alphabets
s = "123Hello123"
print(s.isdecimal())
# contains numbers and spaces
s = "12345 6789"
print(s.isdecimal())
Output
True
False
False
Example 2: String Containing digits and Numeric Characters
The superscript and subscript are considered digit characters and not decimals. If the string contains subscript or superscript the isdecimal()
method will return False.
Similarly, roman numbers, currencies, and fractions are considered numeric numbers and not decimals. The isdecimal()
method will return False
if it finds these characters.
It is recommended to use isdigit() method and isnumeric()
method to check if the characters are valid digits and numeric characters respectively.
# Python3 program to demonstrate the use
# of isdecimal()
# vaid decimal
s = '12345'
print(s.isdecimal())
# in case of digit
#s = '²123'
s = '\u00B2123'
print(s.isdecimal())
# incase of numeric
# s = '½'
s = '\u00BD'
print(s.isdecimal())
Output
True
False
False