Check if a String is Digit in Python

  • Post category:String

In this article, you will see how to check if a string is digit in Python. A digit string is a string that contains only digits. The string isdigit() method returns True if all characters in the string are digits, otherwise False.

Step 1: Create a string

str = "1234567890"
print("str = ", str)
print("str type = ", type(str))

Step 2: Check if a string is a digit

if all(c.isdigit() for c in str):
    print("Digit")
else:
    print("Not Digit")

Output:

str =  1234567890
str type =  <class 'str'>
Digit

Free resources to learn advanced skills: AiHints and CodeAllow