In this tutorial, we’ll learn what Python string isdigit() method is and how to properly use it. We’ll go through multiple Python code examples to understand how isdigit() method works.
Introduction: Python String isdigit() Method
This method will return True if a string consists of only digit characters, or else it will return False.
Syntax of isdigit() Method
string.isdigit()
- Python string isdigit method does not take any arguments.
- This method returns True if the string has only digit characters and will return False if atleast one character is non-digit.
Example 1: Applying isdigit() Method on a Python String
stringVal='234906' print( stringVal.isdigit() )
Output
True
The string has only digit characters present in it so Python string isdigit method return True.
Let’s pass some non-digit characters to string and see what this method will return. See below examples:
print('234 45'.isdigit()) print('234_45'.isdigit()) print('234-45'.isdigit()) print('234,45'.isdigit()) print('234.45'.isdigit()) print('23445st'.isdigit()) print('23445&'.isdigit()) print('@*223445'.isdigit())
Output
False False False False False False False False
A digit is a character having property value:
- Numeric_Type=Digit
- Numeric_Type=Decimal
In Python, superscript and subscripts (usually written using unicode) are also considered digit characters. Hence, if the string contains these characters along with decimal characters, then Python string isdigit() method returns True.
The roman numerals, currency numerators and fractions (usually written using unicode) are considered numeric characters but not digits. Python string isdigit method returns False if the string contains these characters.
Example 2: String Having Numeric and Digit Characters
print('2374'.isdigit()) print('\u00B23455'.isdigit()) // superscript is a digit print('\u00BD'.isdigit()) // fraction is not a digit
Output
True True False
Example 3: isdigit() Method Return Value
strVal='345432' returnedValue=strVal.isdigit() print(returnedValue)
Output
True
Python string isdigit method returns True as the string has only digit characters.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical knowledge of how to properly use Python string isdigit method. I’ll be looking forward to receive your valuable feedback on this post. Thank you for reading it.