Python String isdigit()

Python String isdigit() method returns True if each of the character in given string is only a digit.

Digits are decimal numbers, exponents, subscripts, etc.

If the string contains one or more characters that does not belong to digit group, then isdigit() returns False.

In this tutorial, we will learn the syntax and examples for isdigit() method of String class.

Syntax

The syntax of String isdigit() method in Python is

</>
Copy
str.isdigit()

Example

In this example, we will take a string '53²', and check if this string is decimal using str.isdigit() method.

Python Program

</>
Copy
x = '53²'
result = x.isdigit()
print(result)

Output

True

String contains non-digit character

In this example, we will take a string 'a12', and check if this string contains only digits using str.isdigit() method. Since the string contains non-digit character a, isdigit() returns False.

Python Program

</>
Copy
x = 'a12'
result = x.isdigit()
print(result)

Output

False

Conclusion

In this Python Tutorial, we learned how to check if given string contains only digits, using String method – isdigit().