NumPy Isdecimal Function

NumPyNumPyBeginner
Practice Now

Introduction

In this lab, we will learn about the isdecimal() function of the char module in Numpy library. The isdecimal() function checks whether an element contains only decimal characters or not.

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 necessary packages

First, we need to import the necessary packages which in our case is only NumPy.

import numpy as np

Use isdecimal() with a string

In this step, we will use the isdecimal() function with a simple string.

string1 = "12342"
print("The Input string is:")
print(string1)

x = np.char.isdecimal(string1)
print("The Output is:")
print(x)

Output:

The Input string is:
12342
The Output is:
True

Use isdecimal() with an array of strings

In this step, we will use the isdecimal() function on an array of strings.

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

x = np.char.isdecimal(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]

Summary

In this lab, we learned about the isdecimal() function in the Numpy library. We saw how it is used with its syntax and values returned by this function along with multiple code examples.

Summary

The isdecimal() function in the NumPy library checks whether an element contains only decimal characters or not. This function returns a boolean value where True is returned if the element contains only decimal characters and False is returned if not. It could be used to check if a number is decimal or not using the appropriate characters.

Other NumPy Tutorials you may like