Home »
Python
Python String isdigit() Method
By IncludeHelp Last updated : December 15, 2024
Python String isdigit() Method
The isdigit() is an in-built method in Python, which is used to check whether a string contains only digits or not.
Digit value contains all decimal characters and other digits which may represent power or anything related to this, but it should be a complete digit, like "3".
This method is different from isdecimal() method and isnumeric() method because first method checks only decimal characters and second method checks numeric values including decimal characters. And this (isdigit()) method will consider only digit.
Note
- Digits value contains decimal characters and other Unicode digits, fractional part of the value like ½, ¼ etc are not considered as digit.
- String should be Unicode object - to define a string as Unicode object, we use u as prefix of the string value.
Syntax
String.isdigit();
Parameters
None
Return Type
- true - If all characters of the string are digits then method returns true.
- false - If any of the characters of the string is not a digit then method returns false.
Example 1
# Only digits (decimal characters)
str1 = u"362436"
print (str1.isdigit())
# digit value (no decimals but a digit)
str2 = u"3"
print (str2.isdigit())
# numeric values but not any digit
str3 = u"½¼"
print (str3.isdigit())
#digits, alphabets
str4 = u"Hello3624"
print (str4.isdigit())
Output
True
True
True
False
Example 2
# Example 1
str1 = "12345"
print(str1.isdigit())
# Example 2
str2 = "123abc"
print(str2.isdigit())
# Example 3
str3 = "123 456"
print(str3.isdigit())
# Example 4
str4 = ""
print(str4.isdigit())
# Example 5
str5 = "123$456"
print(str5.isdigit())
Output
True
False
False
False
False
Reference: String isdigit()