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.
Also read How to rename columns in Pandas DataFrame
In this article, we will learn about the Python String isdecimal()
method with the help of examples.
isdecimal() Syntax
The Syntax of isdecimal()
method is:
string.isdecimal()
isdecimal() Parameters
The isdecimal()
method does not take any parameters.
isdecimal() 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()
# 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 as digit characters and not decimals. If the string contains subscript or superscript the isdecimal() method will returns False.
Similarly, the roman numbers, currencies and fractions are considered as numeric numbers and not decimals. The isdecimal() 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
Top comments (0)