How to check if a Python string contains only digits?


To check if a Python string contains only digits, we can use the built-in isdigit() method. This method returns True if all the characters in the string are digits, and False otherwise.

Here's an example code snippet that demonstrates how to use isdigit():

To check if a string contains only digits

Example

In this example, we first define a string called my_string that contains only digits. We then use the isdigit() method to check if my_string contains only digits. Since it does, the output will be "The string contains only digits!".

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

Output

The string contains only digits!

Now let's see an example where the string contains non-digit characters as well:

Example

In this example, the string my_string contains non-digit characters ("a" and "b"). When we run the code, the output will be "The string does not contain only digits." because the isdigit() method returns False.

my_string = "12ab"

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

Output

The string does not contain only digits.

Example

In this example, we first prompt the user to enter a string using the input() function. We then check if the string contains only digits using the isdigit() method. If the string contains only digits, we print "The string contains only digits." Otherwise, we print "The string does not contain only digits."

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.")

Output

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

Example

In this example, we define a function called contains_only_digits() that takes a string as input and checks if it contains only digits using a for loop and the isdigit() method. If the string contains only digits, the function returns True. Otherwise, it returns False.

We then define a string called my_string that contains only digits and call the contains_only_digits() function with my_string as input. Since my_string contains only digits, the output will be "The string contains only digits!".

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.")

Output

The string contains only digits!

Example

In this example, we use the re module to check if a string contains only digits. We define a string called my_string that contains non-digit characters ("a" and "b"). We then use the re.fullmatch() function to check if my_string contains only digits.

The r"\d+" regular expression pattern matches one or more digits. If my_string matches this pattern, it contains only digits and the output will be "The string contains only digits!". Otherwise, the output will be "The string does not contain only 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.")

Output

The string does not contain only digits.

Example

In this example, we use the re module to check if a string contains only digits. We define a string called my_string that contains all digit characters (123456). We then use the re.search() function to check if my_string contains only digits.

The r"\d+" regular expression pattern matches one or more digits. If my_string matches this pattern, it contains only digits and the output will be "The string contains only digits!". Otherwise, the output will be "The string does not contain only digits.".

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

Output

The string contains only digits.

Example

In this example, we use the re module to check if a string contains only digits. We define a string called my_string that contains non-digit characters (ā€œcā€, ā€œdā€). We then use the re.findall() function to check if my_string contains only digits.

The r"\d+" regular expression pattern matches one or more digits. If my_string matches this pattern, it contains only digits and the output will be "The string contains only digits!". Otherwise, the output will be "The string does not contain only digits.".

import re

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

Output

The string does not contain only digits.

Updated on: 10-Aug-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements