Open In App

Python string isdecimal() Method

Improve
Improve
Like Article
Like
Save
Share
Report

Python String isdecimal() function returns true if all characters in a string are decimal, else it returns False. In this article, we will explore further the isdecimal() method, understand its functionality, and explore its practical applications in Python programming.

Python String isdecimal() Syntax

Syntax: string_name.isdecimal(), string_name is the string whose characters are to be checked

Parameters: This method does not takes any parameters .

Return: boolean value. True – all characters are decimal, False – one or more than one character is not decimal.

String isdecimal() in Python Example

Let’s explore some examples to understand how the isdecimal() method works:

Python3




print("100".isdecimal())


Output

True

String Containing digits and Numeric Characters

In Python, we can check if a string contains digit or numeric characters using isdecimal() method. Here is the Program to demonstrate the use of the Python String decimal() Method.

Python3




s = "12345"
print(s.isdecimal())
 
# contains alphabets
s = "12geeks34"
print(s.isdecimal())
 
# contains numbers and spaces
s = "12/34"
print(s.isdecimal())


Output

True
False
False

Converting Numerical Strings to Integers using Isdecimal()

In Python, we can convert a string to an integer using isdecimal() method. Here is the Program to demonstrate the use of the Python String decimal() Method.

Python3




def convert_int(num_str):
    if num_str.isdecimal():
        return int(num_str)
    else:
        return None
 
print(convert_int("555"))   
print(convert_int("11.11")) 


Output

555
None

Difference between isdigit(), isnumeric() and isdecimal()

In Python, the isdigit(), isnumeric(), and isdecimal() methods are used to determine whether a string contains only numeric characters. Although they may appear similar, each method has its own unique characteristics and purpose. Let’s Explore in depth the between these methods:

Difference between isdigit() and isdecimal()

In Python, isdigit() is a method of the str class and returns True if all characters in the string are numeric digits (0 to 9). Here, Python String isdecimal() returns False because not all characters in the “expr” are decimal.

Python3




expr = "4²"
print("expr isdigit()?", expr.isdigit())
 
print("expr isdecimal()?", expr.isdecimal())


Output

expr isdigit()? True
expr isdecimal()? False

Difference between isnumeric() and isdecimal()

In Python, isnumeric() is also a method of the str class, and it returns True if all characters in the string are numeric. Here, Python String isdecimal() returns False because not all characters in the “expr” are decimal.

Python3




expr = "â…”"
print("expr isnumeric()?", expr.isnumeric())
 
print("expr isdecimal()?", expr.isdecimal())


Output

expr isnumeric()? True
expr isdecimal()? False


Last Updated : 14 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads