DEV Community

Cover image for Numpy Isnumeric Function: Mastering Numeric String Validation
Labby for LabEx

Posted on • Originally published at labex.io

Numpy Isnumeric Function: Mastering Numeric String Validation

Introduction

In this lab, we will cover the isnumeric() function of the char module in the Numpy library. This function is used to check if a string contains only numeric characters. The function returns True if there are only numeric characters in the string, otherwise, it returns False.

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 Numpy Library

We need to import the numpy library before we can use the isnumeric() function. We use the import keyword followed by the library name numpy and the nickname np:

import numpy as np
Enter fullscreen mode Exit fullscreen mode

Using isnumeric() with a Single String

We can use the isnumeric() function to check if a single string contains only numeric characters. Let's use an example string "12Apple90" and apply the isnumeric() function to it:

import numpy as np

string1 = "12Apple90"
print("The Input string is:")
print(string1)

x = np.char.isnumeric(string1)
print("The Output is:")
print(x)
Enter fullscreen mode Exit fullscreen mode

Output:

The Input string is:
12Apple90
The Output is:
False
Enter fullscreen mode Exit fullscreen mode

As we can see, the isnumeric() function returns False as there are non-numeric characters in the input string.

Using isnumeric() with an Array

We can also use the isnumeric() function with an array of strings. Let's use an example array inp_ar which contains a mixture of numeric and non-numeric strings:

import numpy as np

inp_ar = np.array(['1', '2000', '90', '3.5', '0'])
print("The Input array is:")
print(inp_ar)

outp_arr = np.char.isnumeric(inp_ar)
print("The Output array is:")
print(outp_arr)
Enter fullscreen mode Exit fullscreen mode

Output:

The Input array is:
['1' '2000' '90' '3.5' '0']
The Output array is:
[ True  True  True False  True]
Enter fullscreen mode Exit fullscreen mode

As we can see, the isnumeric() function returns an array of boolean values with True indicating that the string contains only numeric characters and False indicating that the string contains non-numeric characters.

Limitations of isnumeric()

It is important to note that the isnumeric() function returns False for a string with a numeric value with a decimal, as shown in Example 2 above.

Summary

In this lab, we learned about the isnumeric() function of the Numpy library. We covered how to use it with single strings and arrays, as well as the limitations of the function.


Want to learn more?

Join our Discord or tweet us @WeAreLabEx ! 😄

Top comments (0)