In this post, we are going to see the datatype of the numpy object when the underlying data is of string type. In numpy, if the underlying data type of the given object is string then the dtype of object is the length of the longest string in the array. This is so because we cannot create variable length string in numpy since numpy needs to know how much space should be allocated for string.
Problem #1 : Given a numpy array whose underlying data is of string type. Find the dtype.
Solution : We will use numpy.dtype
attribute to check the dtype of the given object.
import numpy as np
arr = np.array([ 'Ela' , 'Ed' , 'Brook' , 'Sia' , 'Katherine' ])
print (arr)
|
Output :

Now we will check the dtype of the given array object whose underlying data is of string type.
Output :

As we can see in the output, the dtype of the given array object is '<U9'
where 9 is the length of the longest string in the given array object.
Let’s verify this by checking the length of the longest string in the given object.
length_checker = np.vectorize( len )
arr_len = length_checker(arr)
print (arr_len)
print (arr_len. max ())
|
Output :


Problem #2 : Given a numpy array whose underlying data is of string type. Find the dtype.
Solution : We will use numpy.dtype
attribute to check the dtype of the given object.
import numpy as np
arr = np.array([ 'New York' , 'Lisbon' , 'Beijing' , 'Quebec' ])
print (arr)
|
Output :

Now we will check the dtype of the given array object whose underlying data is of string type.
Output :

As we can see in the output, the dtype of the given array object is '<U8'
where 8 is the length of the longest string in the given array object.
Let’s verify this by checking the length of the longest string in the given object.
length_checker = np.vectorize( len )
arr_len = length_checker(arr)
print (arr_len)
print (arr_len. max ())
|
Output :

