w3resource

NumPy String: numpy.char.isdigit() function

numpy.char.isdigit() function

The numpy.char.isdigit() function returns true for each element if all characters in the string are digits and there is at least one character, false otherwise.

This function is useful for checking if a string contains only digits or not. It can be used for data validation, data cleaning, and data manipulation.

Syntax:

numpy.char.isdigit(a)

Parameter:

Name Description Required /
Optional
a: array_like, unicode Input an array_like of string or unicode. Required

Return value:

Output array of bools

Example: Checking if a string contains only digits using numpy.char.isdigit()

>>> import numpy as np
>>> x = np.char.isdigit('123')
>>> x
array(True)

In the above example, since the string '123' contains only digits, the function returns an array with a single True value.

Pictorial Presentation:

NumPy String operation: isdigit() function

Example: Testing if a string consists of digits only

>>> import numpy as np
>>> y = np.char.isdigit('abc')
>>> y
array(False)

In the above example, the input string is 'abc', which does not consist of digits only, so the resulting boolean array contains False values.

Pictorial Presentation:

NumPy String operation: isdigit() function

Example: Testing for numeric characters using numpy.char.isdigit()

>>> import numpy as np
>>> z = np.char.isdigit('123abc')
>>> z
array(False)

In the above example, the input string '123abc' contains both numeric and non-numeric characters, so the output is a boolean value of False.

Pictorial Presentation:

NumPy String operation: isdigit() function

Python - NumPy Code Editor:

Previous: isdecimal()
Next: islower()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/numpy/string-operations/isdigit.php