How to check if a string contains only decimal characters?

In many programming scenarios, it is common to validate user input to ensure it contains the specified value. For example, when accepting age, pin-code or numbers from the user, we make sure the input entered is completely digits. One specific way to do this is by checking if the string contains only decimal characters.

Decimal characters are the characters used to write base-10 numbers (the digits from 0-9). Python provides built-in methods to perform this check easily. Let's dive into the article to learn more about it.

Using isdecimal() Method

The Python isdecimal() method is used to check whether the string contains only decimal digits or not. It returns True if all characters are decimal digits (0-9), otherwise False.

The isdecimal() method is applicable only on Unicode string objects.

Syntax

str.isdecimal()

Example 1 ? Basic Usage

Let's look at the following example where we check a string containing only digits ?

demo = "112321"
print(demo.isdecimal())
True

Example 2 ? String with Mixed Characters

Consider another scenario where we check a string with numbers along with text ?

demo = "112ACD"
print(demo.isdecimal())
False

Using isdigit() Method

The Python isdigit() method is used to check whether the string consists of digits or not. It returns True if all characters in the input string are digits, otherwise False. Unlike isdecimal(), it also accepts superscript digits.

Syntax

str.isdigit()

Example

In the following example, we use the isdigit() method to check whether the string contains only digits ?

demo = "112332"
print(demo.isdigit())

# Testing with superscript
superscript = "123²"
print(superscript.isdigit())
True
True

Using isnumeric() Method

The Python isnumeric() method is used to check whether the string consists of numeric characters. It returns True if all characters in the input string are numeric, otherwise False. This is the most inclusive method, accepting digits, superscripts, fractions, and Roman numerals.

Syntax

str.isnumeric()

Example

Following is the example where we use the isnumeric() method ?

test = "12312"
print(test.isnumeric())

# Testing with fraction
fraction = "½"
print(fraction.isnumeric())
True
True

Using Regular Expressions

Another way to achieve this is using Regular Expressions. The pattern ^\d+$ checks if the string contains only digits from start to end.

Example

In the example below, we use regular expressions to check if the given string contains only decimal characters ?

import re

str1 = "12345"
str2 = "123@45"

print("Checking if the string '", str1, "' has only decimal characters")
print(bool(re.match(r"^\d+$", str1)))

print("Checking if the string '", str2, "' has only decimal characters")  
print(bool(re.match(r"^\d+$", str2)))
Checking if the string ' 12345 ' has only decimal characters
True
Checking if the string ' 123@45 ' has only decimal characters
False

Comparison

Method Decimal Digits (0-9) Superscript Digits Fractions Best For
isdecimal() ? ? ? Strict decimal validation
isdigit() ? ? ? Digits including superscripts
isnumeric() ? ? ? Any numeric characters
Regular Expressions ? ? ? Custom patterns

Conclusion

Use isdecimal() for strict decimal digit validation (0-9 only). Use isdigit() if you need to include superscript digits, and isnumeric() for the broadest numeric character validation. Regular expressions provide custom pattern matching for specific requirements.

Updated on: 2026-03-24T16:36:18+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements