Python Tutorial Python Advanced Python References Python Libraries

Python String - isdigit() Method



The Python isdigit() method is used to check whether all characters of the string are digits or not. It returns true when all characters of the string are digits, else returns false. Digit characters belongs to numbers 0 - 9 and exponential format (example- 5²).

Syntax

 string.isdigit() 

Return Value

Returns True if all characters of the specified string are digit, else returns False.

Example:

In the example below, isdigit() method is used to check whether all characters of the string are digits or not.

MyString = "12345" print(MyString.isdigit()) MyString = "5²" print(MyString.isdigit()) 

The output of the above code will be:

 True True 

Example:

In the example below, isdigit() method is used to check whether all characters of the unicode string object are digits or not.

MyString = "\u0035" #unicode for 5 print(MyString.isdigit()) MyString = "\u0035\u00B2" #unicode for 5² print(MyString.isdigit()) 

The above code will give the following output:

 True True 





❮ Python String Methods