How to check if a Python string contains only digits?



To check if a Python string contains only digits, you can use the built-in isdigit() method. This method returns True if all characters in the string are digits, and False otherwise. Besides isdigit(), you can also use loops or regular expressions to verify this condition.

Using the isdigit() Method

The isdigit() method is used to check if a string contains only digits. It returns True only when every character is a digit.

Example

In this example, we define a string that contains only digits and use the isdigit() method to check it. The output confirms that the string contains only digit -

my_string = "1234"
if my_string.isdigit():
   print("The string contains only digits!")
else:
   print("The string does not contain only digits.")

Following is the output obtained -

The string contains only digits!

Using isdigit() with Non-Digit Characters

This method returns False if any character in the string is not a digit. Even a single non-digit character will cause it to fail.

Example

Here, the string contains non-digit characters ("a" and "b"). The isdigit() method returns False, so the output indicates the string does not contain only digits -

my_string = "12ab"
if my_string.isdigit():
   print("The string contains only digits!")
else:
   print("The string does not contain only digits.")

We get the output as shown below -

The string does not contain only digits.

Using User Input and isdigit() Function

You can check if the user input contains only digits by applying the isdigit() function on the input string. This is useful for validating numeric input.

Example

This example prompts the user to enter a string and then checks if it contains only digits using the isdigit() function -

input_str = input("Enter a string: ")
if input_str.isdigit():
   print("The string contains only digits.")
else:
   print("The string does not contain only digits.")

The result produced is as follows -

Enter a string: 357sfy
The string does not contain only digits.

Checking Digits with a Custom Function

A custom function can iterate over each character and verify if it is a digit. This gives more control if you want to extend the check later.

Example

In this example, we create a function that iterates over each character of the string and uses isdigit() function to check if all characters are digits. The function returns True or False accordingly -

def contains_only_digits(input_str):
   for char in input_str:
      if not char.isdigit():
         return False
   return True

my_string = "1234"
if contains_only_digits(my_string):
   print("The string contains only digits!")
else:
   print("The string does not contain only digits.")

The output obtained is as shown below -

The string contains only digits!

Using Regular Expressions with re.fullmatch()

The re.fullmatch() function checks if the entire string matches the digit pattern. This approach is useful for pattern matching.

Example

This example uses the re module to check if the string contains only digits by matching it fully with the pattern r"\d+", which means one or more digits -

import re
my_string = "12ab"
if re.fullmatch(r"\d+", my_string):
   print("The string contains only digits!")
else:
   print("The string does not contain only digits.")

Following is the output obtained -

The string does not contain only digits.

Using re.search() to Find Digits

The re.search() function looks for a pattern anywhere in the string. It can find digits but does not ensure the whole string is digits only.

Example

Here, re.search() function is used to check if there is any digit in the string. However, this only checks for presence of digits, not if the entire string consists only of digits -

import re
my_string = "123456."
if re.search(r"^\d+$", my_string):
   print("The string contains only digits.")
else:
   print("The string does not contain only digits.")

We get the output as shown below -

The string does not contain only digits.

Using re.findall() to Detect Digits

The re.findall() function returns all digit sequences in the string. This helps detect presence of digits but doesn't confirm if the string is digits only.

Example

This example uses re.findall() function to find all digit sequences in the string. If the entire string is digits only, the matches should cover the whole string -

import re

my_string = "5678cd."
if re.findall(r"\d+", my_string):
   print("The string contains digits.")
else:
   print("The string does not contain digits.")		

The output obtained is as shown below -

The string contains digits.
Updated on: 2025-09-02T13:15:29+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements