.isdigit()
Published Sep 26, 2023
Contribute to Docs
The .isdigit()
method under the string class checks if all the elements in the string are digits; applicable elements also include special cases like compatibility superscript digits (¹, ², ³, etc.). The method returns True
in the mentioned cases and must not be an empty string, otherwise, it returns False
.
Syntax
string.isdigit()
Example
The code below uses .isdigit()
to check if a string contains only digits:
my_string1 = "123"my_string2 = "123AA"print(my_string1.isdigit())print(my_string2.isdigit())
This results in the following output:
TrueFalse
Codebyte Example
The code below is runnable and uses .isdigit()
to check my_string
:
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.