In this tutorial, we’ll learn what Python string isdecimal() method is and how to properly use it. We’ll go through multiple detailed Python code examples to understand how isdecimal method works.
Introduction: Python String isdecimal() Method
This method is used to return True if a string has only decimal characters, or else it will return False.
Syntax of isdecimal() Method
string.isdecimal()
- This method does not take any arguments.
- Python string isdecimal() method return True if the string has only decimal characters. And it will return False if atleast one character is non decimal.
Example 1: isdecimal() Method Applied on a String
print('234'.isdecimal()) print('234.4'.isdecimal()) print('23 4'.isdecimal()) print('234s'.isdecimal()) print('23$4'.isdecimal()) print('23_4'.isdecimal())
Output
True False False False False False
We can see that only first statement returns True. All the others have some non-decimal characters present in it, so they return False.
The subscripts and superscript are considered as digit characters(but not decimals). If a specific string includes these characters (usually written using unicode), then Python string isdecimal() method will return False.
Similarly, currency numerators, roman numerals, and fractions are also considered numeric numbers (usually written using unicode) but not decimals. Python string isdecimal() method will return False if a string containing these characters are passed to it.
Example 2: isdecimal() Method Applied on a String(containing Digits and Numeric Characters)
print('239455'.isdecimal()) print('²73382'.isdecimal()) print('½737373'.isdecimal())
Output
True False False
Example 3: isdecimal() Method Return Value
val='7723947' returnedVal=val.isdecimal() print(returnedVal)
Output
True
We can see that Python string isdecimal method returns True as the string has only decimal characters.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical knowledge of how to properly use Python string isdecimal method. I’ll love to receive your valuable feedback on this post. Thank you for reading it.