Python String isdecimal() Method



The Python string isdecimal() method checks whether the string consists only decimals (0-9) or not. This method is applicable only on Unicode objects.

But what are Unicode objects? In python, Unicode strings or Unicode objects handle the complete range of Unicode characters while using the memory efficiently. These Unicode strings are either 16-bit or 32-bit integers depending on the type of compiler or interpreter used. They also have the same methods as the normal Python strings.

Note − To define a string as Unicode, one simply prefixes a 'u' to the opening quotation mark of the assignment. We will see the examples further in the article.

Syntax

Following is the syntax for Python String isdecimal() method −

str.isdecimal()

Parameters

This method does not accept any parameters.

Return Value

This method returns true if all characters in the string are decimal, false otherwise. However, there are some special cases; they are −

  • If the decimals are present in superscript or subscript, the method returns false
  • If the argument given is either roman numerals or currency numerals, the method returns false

Example

When the given string consists all decimal characters, the method returns true.

In the following example, we create a string containing decimal characters only. The Python String isdecimal() method is called on this string and the return value is obtained as True.

str = u"23443434"
print(str.isdecimal())

When we run above program, it produces following result −

True

Example

When the given string consists all decimal characters, the method returns true.

In this example, we create a string that contains characters other than the decimal characters as input. The isdecimal() method is then called on this string and the return value will be obtained as False.

str = u"this2009" 
print(str.isdecimal())

When we run above program, it produces following result −

False

Example

If the string contains decimals in either superscript or subscript, the method returns false.

In the example below, the input string contains all decimal values but it includes decimals in superscript, in the form of Unicode code points. The isdecimal() method is called on this string and the return value will be obtained as False.

str = u"\u20783434"
print(str.isdecimal())

Once we compile and run the program, the output is obtained as follows −

False

Example

If the string contains either Roman numbers or currency numbers, the method returns false.

Following example takes an input string with Unicode code points of Roman numerals as its characters. The method is called on this string and the return value is obtained as False.

str = u"\u21623434"
print(str.isdecimal())

Compile and run the program to produce the following result −

False
python_strings.htm
Advertisements