Signup/Sign In

NumPy isalpha() function

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

The isalpha() function returns True if all the characters in the string element are alphabets, otherwise, this function will return False.

This function calls str.isalpha internally for each element of the array.

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

  • In case if the element contains mixed characters(alphabets and digits) then this function will return False.

  • If there are whitespaces in a string then also this function returns False.

Syntax of isalpha():

The syntax required to use this function is as follows:

numpy.char.isalpha(arr)

The above syntax indicates that isalpha() 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 is in uppercase or not.

Example 1: With an array of strings

Let's take the first example with an array of strings:

import numpy as np

inp_ar = np.array([ 'Ram', 'Mohan', 'Apple9','Chair s'] )
print("The Input string is:")
print(inp_ar)

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


The Input string is:
['Ram' 'Mohan' 'Apple9' 'Chair s']
The Output is:
[ True True False False]

Example 2: With an array of alpha-numeric values

In the below code snippet, we will use isalpha() function with alpha-numeric values in an array:

import numpy as np

inp_ar = np.array([ 'Superb !', 'Amazing!', 'fab','cool123'] )
print("The Input string is:")
print(inp_ar)

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


The Input string is:
['Superb !' 'Amazing!' 'fab' 'cool123']
The Output is:
[False False True False]

Summary

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



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.