Signup/Sign In
LAST UPDATED: DECEMBER 10, 2020

NumPy isdigit() function

In this tutorial, we will cover isdigit() function of the char module of the Numpy library.

The isdigit() function returns True if all the characters in the element are digits or numbers otherwise, this function returns False.

  • This function calls str.isdigit on every element of the array.

  • This function is locale-dependent for an 8-bit string.

Syntax of numpy.char.isdigit():

The syntax required to use this function is as follows:

numpy.char.isdigit(arr)

The above syntax indicates that isdigit() function takes a single parameter.

In the above syntax, the argument arr is mainly used to indicate the input array of strings on which this function will be applied.

Returned Values:

This function will return an output array of boolean values, with True and False values corresponding to every string element, based on whether the string contains only digits(number) or not.

Example 1:

Below we have a simple code example, where we have used the isdigit() function with an array of strings, some of which only contain digit, where as some have other characters too:

import numpy as np

inp_ar = np.array([ '20002', '10009', '12345ab','01'] )
print("The Input string is:")
print(inp_ar)

x = np.char.isdigit(inp_ar)
print("The Output is:")
print(x)


The Input string is:
['20002' '10009' '12345ab' '01']
The Output is:
[ True True False True]

Example 2:

In the code snippet we will use isdigit() function with array of strings, in which some strings have whitespaces and other characters(other than digits):

import numpy as np

inp_ar = np.array([ '20002 2', 'a10009', '12345 ab','01'] )
print("The Input string is:")
print(inp_ar)

x = np.char.isdigit(inp_ar)
print("The Output is:")
print(x)


The Input string is:
['20002 2' 'a10009' '12345 ab' '01']
The Output is:
[False False False True]

Summary

In this tutorial we learned isdigit() function of the Numpy library. We covered how it is used with its syntax and values returned by this function along with some examples where we used the function.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.