.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:

True
False

Codebyte Example

The code below is runnable and uses .isdigit() to check my_string:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy