Python | dtype object length of Numpy array of strings
Last Updated :
14 Mar, 2019
Improve
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
Python3 1==
Output :
Now we will check the dtype of the given array object whose underlying data is of string type.
Python3
Output :
As we can see in the output, the dtype of the given array object is
Python3
Output :
Problem #2 : Given a numpy array whose underlying data is of string type. Find the dtype.
Solution : We will use
Python3 1==
Output :
Now we will check the dtype of the given array object whose underlying data is of string type.
Python3
Output :
As we can see in the output, the dtype of the given array object is
Python3
Output :
numpy.dtype
attribute to check the dtype of the given object.
# importing the numpy library as np
import numpy as np
# Create a numpy array
arr = np.array(['Ela', 'Ed', 'Brook', 'Sia', 'Katherine'])
# Print the array
print(arr)

# Print the dtype
print(arr.dtype)

'<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.
# Use vectorize function of numpy
length_checker = np.vectorize(len)
# Find the length of each element
arr_len = length_checker(arr)
# Print the length of each element
print(arr_len)
# Print the maximum value
print(arr_len.max())


numpy.dtype
attribute to check the dtype of the given object.
# importing the numpy library as np
import numpy as np
# Create a numpy array
arr = np.array(['New York', 'Lisbon', 'Beijing', 'Quebec'])
# Print the array
print(arr)

# Print the dtype
print(arr.dtype)

'<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.
# Use vectorize function of numpy
length_checker = np.vectorize(len)
# Find the length of each element
arr_len = length_checker(arr)
# Print the length of each element
print(arr_len)
# Print the maximum value
print(arr_len.max())

