Python String isdigit() Function

Python String isdigit() function checks for the Digit characters in a string and returns True if the string consists of only digit characters.

Key Points:

  • Return Type: Boolean i.e. True or False
  • Parametric Values: No parameters need to be parsed in isdigit() function
  • Blank spaces in between digits lead to return False
  • Empty String also returns False

String isdigit() Syntax

str_name.isdigit()

str_name here refers to the input string. And, isdigit() is inbuilt string function in python.

str_name = "12345"
print(str_name.isdigit())   # True

String isdigit() Examples

Different cases are given below.

Case 1: String contains whitespaces

str_name = "12 34"
print(str_name.isdigit())   # False

Case 2: String contains alphabets

str_name = "Abc123"
print(str_name.isdigit())   # False

str_name = "Abc"
print(str_name.isdigit())   # False

Case 3: String contains special characters

str_name = "@123"
print(str_name.isdigit())   # False

str_name = "@$&"
print(str_name.isdigit())   # False

Case 4: String contains decimals

str_name = "16.7"
print(str_name.isdigit())   # False

Case 5: String is empty

str_name = ' '
print(str_name.isdigit())   # False

Program to Print List of All Possible Digit Characters in Python

The Unicode module can be used to check the digit characters. The program is to print all digit Unicode characters.

import unicodedata

total_count = 0
for i in range(2 ** 16):
    charac = chr(i)
    if charac.isdigit():
        print(u'{:04x}: {} ({})'.format(i, charac, unicodedata.name(charac, 'UNNAMED')))
        total_count = total_count + 1
print("Total Count of Unicode Digit Characters = ",total_count)
Output All Digit Unicode Characters
Output All Digit Unicode Characters

It is just a glance of output as the actual output is lengthy. There are 445 digit characters in Unicode.


References