w3resource

NumPy String: numpy.char.isalpha() function

numpy.char.isalpha() function

The numpy.char.isalpha() function is used to check whether each character in a string is an alphabet or not

This function is useful in text processing tasks such as data cleaning, text mining, and natural language processing.

Syntax:

numpy.char.isalpha(a)

Parameter:

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

Return value:

Output array of bools

Example: Checking if string contains alphabets or not

>>> import numpy as np
>>> x = np.char.isalpha('Hello')
>>> x
array(True)

In the above example, the string "Hello" is passed as an argument to the isalpha() function which returns True as all the characters in the string are alphabets.

Pictorial Presentation:

NumPy String operation: isalpha() function

Example: Checking if a string is alphanumeric or not using numpy.char.isalpha()

>>> import numpy as np
>>> y = np.char.isalpha('123')
>>> y
array(False)

In the second example, the string '123' is passed to the function, and since it does not contain any alphabetic character, the function returns False.

Pictorial Presentation:

NumPy String operation: isalpha() function

Example: Checking if a string contains alphabets only using numpy.char.isalpha()

>>> import numpy as np
>>> z = np.char.isalpha('abc123')
>>> z
array(False)

In the third example, the input string 'abc123' contains alphabetic characters as well as non-alphabetic characters, so the the function returns False.

Pictorial Presentation:

NumPy String operation: isalpha() function

Python - NumPy Code Editor:

Previous: index()
Next: isdecimal()



Follow us on Facebook and Twitter for latest update.