Python string isdigit() method returns True if all characters in the string are digits; otherwise, returns False.
Syntax
string.isdigit()
Parameters
The function doesn’t take any parameters. There will error shown if we try to pass any parameter to the method.
Return value
It returns True if the string consists of only digits,
It returns False if the string doesn’t contain digits or characters other than digits, like numerical or special characters. When it identifies a space, also it returns false.
We can use the ascii values of characters and print the digits using isdigit().
Visual Representation
Example 1: How to Use String isdigit() Function
string_variable = “1234”
print(string_variable.isdigit())
Output
True
Example 2: String Containing digits and Numeric Characters
s1 = '$2100'
print(s1.isdigit())
s2 = '1900 '
print(s2.isdigit())
num = '\u0038'
print(num.isdigit())
mystr = '\u00380061'
print(mystr.isdigit())
arabic_six = '۶'
print(arabic_six.isdigit())
Output
False
False
True
True
True
Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog. He is also expert in JavaScript and Python development.