Check if a String Contains Only Decimal Characters



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

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

Using Python isdecimal() Method

The Python isdecimal() method is used to check whether the string contains the decimals or not.

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

Syntax

Following is the syntax of Python isdecimal() method ?

str.isdecimal()

Example 1

Let's look at the following example, where we are going to consider the basic usage of the .isdecimal() method.

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

Output

Output of the above program is as follows ?

True

Example 2

Consider another scenario, Where we are going to consider the string with number along with text and observing the output.

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

Output

Output of the above program is as follows ?

False

Using Python isdigit() Method

The Python isdigit() method is used to check whether the string consists of the digits or not. It returns true if all the characters in the input string are digits or else it returns false.

Syntax

Following is the syntax of Python isdigit() method ?

str.isdigit()

Example

In the following example, we are going to use the .isdigit() method, to check whether the string contains digits or not.

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

Output

Following is the output of the above program ?

True

Using Python isnumeric() Method

The Python isnumeric() method is used to check whether the string consists of numeric characters. It returns true if all the characters in the input string are numeric or else it returns false.

Syntax

Following is the syntax of Python isnumeric() method ?

str.isnumeric()

Example

Following is the example, where we are going to use the isnumeric() method and observing the output.

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

Output

Following is the output of the above program ?

True

Using regular Expressions

Another way to achieve this is using Regular Expressions. The regular expression "^\d+?\.\d+?$" is used for checking if there are only digits present. The regular expression function match is used for checking.

Example

In the example given below, we are using regular expressions and finding out whether the given string contains only decimal characters.

import re
str1 = "123.45"
str2 = "123@45"
print("Checking if the string '", str1, "' has only decimal characters")
print(bool(re.match(r"^\d+?\.\d+?$", str1)))
print("Checking if the string '", str2, "' has only decimal characters")
print(bool(re.match(r"^\d+?\.\d+?$", str2)))

Output

Following is the output of the above program ?

Checking if the string ' 123.45 ' has only decimal characters
True
Checking if the string ' 123@45 ' has only decimal characters
False
Updated on: 2025-04-14T11:46:15+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements