Python String isdecimal() Method

Python String isdecimal() method “checks if all the characters in the string are decimal (0-9) characters”. If all characters in the string are decimal characters, the method returns True. Otherwise, it returns False.

Syntax

string.isdecimal()

The isdecimal() function doesn’t take any parameters. Therefore, an error will be shown if we try to pass any parameter to the method.

Return value

It returns True if the string consists only of decimals.

It returns False if the string doesn’t contain decimals or contains characters other than decimals, like numerical or special characters. When it identifies a space, then also it returns False.

Example 1

string = "123456"

print(string.isdecimal())

Output

True 

Example 2

string = "123456"

string2 = "123 456"

print(string.isdecimal())

print(string2.isdecimal())

Output

True 

False

The superscript and subscripts are considered digit characters but not decimals. If the string contains these characters (usually written using Unicode), isdecimal() returns False.

Example 3

string = "123456"

string2 = "19k21k"

string3 = "11m 21k"

print(string.isdecimal())
print(string2.isdecimal())
print(string3.isdecimal())

Output

True 
False 
False

Similarly, roman numerals, currency numerators, and fractions are considered numeric numbers (usually written using Unicode) but not decimals. Therefore, the isdecimal() also returns False.

Two methods, isdigit() and isnumeric(), check whether the string contains digit and numeric characters, respectively.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.