NumPy Isalpha Function

NumPyNumPyBeginner
Practice Now

Introduction

In this lab, we will learn about the isalpha() function in the char module of the NumPy library. The isalpha() function checks if the characters in a string element are alphabets or not. If the characters are all alphabets, the function returns True, otherwise, it returns False. We can apply this function to an entire array of strings.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import the Required Libraries

To use the isalpha() function, we first need to import the NumPy library:

import numpy as np

Create an Array of Strings

Next, let's create an array of strings to use in our examples:

inp_ar = np.array(['Ram', 'Mohan', 'Apple9', 'Chair s'])

Apply isalpha() to the Array of Strings

We can apply the isalpha() function to an array of strings using the following syntax:

x = np.char.isalpha(inp_ar)

This will return an array of boolean values indicating whether each string element in the input array is an alphabetical string or not.

Print the Input and Output

Now that we have applied the isalpha() function to our input array, let's print both the input and output arrays to see the final result:

print("The Input string is:")
print(inp_ar)

print("The Output is:")
print(x)

Example 1 - Check for Alphabetical Strings

Let's apply the isalpha() function to an array of strings containing only alphabetical elements. Here's the code:

inp_ar = np.array(['Ram', 'Mohan', 'Sam', 'John'])
x = np.char.isalpha(inp_ar)
print("The Input string is:")
print(inp_ar)
print("The Output is:")
print(x)

The output will be:

The Input string is:
['Ram' 'Mohan' 'Sam' 'John']
The Output is:
[ True  True  True  True]

Example 2 - Check for Non-Alphabetical Strings

Now let's apply the isalpha() function to an array of strings containing non-alphabetical elements. Here's the code:

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

The output will be:

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

Example 3 - Ignore Whitespaces

The isalpha() function also considers whitespaces as non-alphabetic characters. So, if a string element contains whitespaces, the function will return False. Here's the code to test this:

inp_ar = np.array(['Ram', 'Mohan', 'Hello World'])
x = np.char.isalpha(inp_ar)
print("The Input string is:")
print(inp_ar)
print("The Output is:")
print(x)

This will produce the following output:

The Input string is:
['Ram' 'Mohan' 'Hello World']
The Output is:
[ True  True False]

Example 4 - Mixed Character Strings

Another scenario to consider is when a string element contains both letters and digits. In this case, the isalpha() function will return False. Here's the code to test this:

inp_ar = np.array(['Ram', 'Mohan', 'Apple9'])
x = np.char.isalpha(inp_ar)
print("The Input string is:")
print(inp_ar)
print("The Output is:")
print(x)

This will produce the following output:

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

Summary

In this lab, we learned how to use the isalpha() function of the NumPy library to check if the string elements in an input array are alphabetic or not. We applied this function to different arrays of strings containing alphabetical and non-alphabetical elements, as well as mixed characters and whitespaces, to observe the output.

Summary

Congratulations! You have completed the NumPy Isalpha() Function lab. You can practice more labs in LabEx to improve your skills.

Other NumPy Tutorials you may like