Python String isdigit() Method



The python string isdigit() method is used to check whether the string consists of digits. This method returns true if all the characters in the input string are digits and there is atleast one character. Otherwise, it returns false.

Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. 𐩀,𐩂,𐩃,𐩄,𐩅,𐩆,𐩇 are Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

Let us look into this method with more details in the following section.

Syntax

Following is the syntax for the python string isdigit() method −

str.isdigit()

Parameters

The python string isdigit() method doesnot contain any parameters.

Return Value

The python string isdigit() method returns true if all characters in the string are digits and there is at least one character and false otherwise.

Example

The following is an example of the python string isdigit() method. In this program, a string "1235" is created and the isdigit() method is invoked.

str = "1235"
result=str.isdigit()
print("Are all the characters of the string digits?", result)

On executing the above program, the following output is generated -

Are all the characters of the string digits? True

Example

In the python string isdigit() method, the digits also include the kharosthi numbers which are 𐩀,𐩂,𐩃,𐩄,𐩅,𐩆,𐩇.

str = "𐩁𐩂𐩃"
result=str.isdigit()
print("Are all the characters of the string digits?", result)

The following is the output obtained by executing the above program -

Are all the characters of the string digits? True

Example

The superscripts of numbers in unicode representation are also considered as digits by this isdigit() method.

str = "\u00B3" #superscript of 3
result=str.isdigit()
print("Are all the characters of the string digits?", result)

The following output is obtained by executing the above program -

Are all the characters of the string digits? True

Example

The isdigit() method considers unicode representation of numbers as digits only.

str = "\u0030" #unicode for 0
result=str.isdigit()
print("Are all the characters of the string digits?", result)

The above program, on executing, displays the following output -

Are all the characters of the string digits? True

Example

The fractions are not considered as digits. So, if the created string contains fractions in unicode format or any other format, the method returns false.

str = "\u00BE" #unicode for fraction 3/4
result=str.isdigit()
print("Are all the characters of the string digits?", result)

The output of the above program is displayed as follows -

Are all the characters of the string digits? False
python_strings.htm
Advertisements