numpy - isalpha() and isalnum()
In this numpy tutorial, we will discuss isalpha() and isalnum() functions performed on numpy array.
Introduction
numpy stands for numeric python which is used to perform mathematical operations on arrays.
It is a module in which we have to import from the python.
Syntax to import:
import numpy
We can also use alias for the module
For example,
import numpy as np
We can directly use np to call the numpy module.
Array
An array is an one dimensional data structure used to store single data type data.
I.E It will only store all integer data or all string type data.or all float type data.
We can create an numpy array by using array() function.
Syntax:
numpy.array(elements)
where, elements are the input data elements.
This method will check each and every element in numpy array and if there are only alphabets present in element, it will return True, otherwise False.
Syntax:
numpy.char.isalpha(array_input)
where, array_input is the numpy input array
Example:
In this example, we will create an numpy array and apply isalpha() on the array
#importing numpy module
import numpy
#create numpy array with 5 string elements
data=numpy.array(['php',"html css","jsp","go lang","css/jsp"])
#display the array
print(data)
#isalpha()
print(numpy.char.isalpha(data))
Output:
We observed that the elements php and jsp contains only alphabets so it will return True and remaining contains spaces or '/' symbols which are not alphabets, hence it returned False for these elements.
['php' 'html css' 'jsp' 'go lang' 'css/jsp']
[ True False True False False]
This method will check each and every element in numpy array and if there are only alphabets or numbers present in element, it will return True, otherwise False.
Syntax:
numpy.char.isalnum(array_input)
where, array_input is the numpy input array
Example:
In this example, we will create an numpy array and apply alnum()
#importing numpy module
import numpy
#create numpy array with 5 string elements
data=numpy.array(['ph7p',"html90css","jsp","go lang","css/jsp"])
#display the array
print(data)
#isalnum()
print(numpy.char.isalnum(data))
Output:
We observed that the elements 'go lang' 'css/jsp' contains space and '/'. so it will return False as they are neither alphabet nor number and remaining contains only alphabets and numbers hence it returned True for these elements.
['ph7p' 'html90css' 'jsp' 'go lang' 'css/jsp']
[ True True True False False]
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University.
Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :
Published Date :
Jun 14,2024