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)
Output:
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)
Output:
The Input string is:
['20002 2' 'a10009' '12345 ab' '01']
The Output is:
[False False False True]