Python | dtype object length of Numpy array of strings
Last Updated :
14 Mar, 2019
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.
Python3 1==
# 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)
Output :

Now we will check the dtype of the given array object whose underlying data is of string type.
Python3
# Print the dtype
print(arr.dtype)
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.
Python3
# 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())
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.
Python3 1==
# 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)
Output :

Now we will check the dtype of the given array object whose underlying data is of string type.
Python3
# Print the dtype
print(arr.dtype)
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.
Python3
# 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())
Output :
Similar Reads
Data type Object (dtype) in NumPy Python Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about: Type of the data (integer, float, Python object, etc.)Size of the data (number of bytes)The byte order of the data (little-endia
3 min read
numpy.array_str() in Python numpy.array_str()function is used to represent the data of an array as a string. The data in the array is returned as a single string. This function is similar to array_repr, the difference being that array_repr also returns information on the kind of array and its data type. Syntax : numpy.array_st
2 min read
Store Different Datatypes In One Numpy Array Storing diverse data types in a single NumPy array presents an effective approach to handling varied datasets efficiently. Although NumPy arrays are commonly homogeneous, situations may arise where managing multiple data types within a single array becomes necessary. In this article, we will underst
3 min read
NumPy - Data type Objects(dtype) Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about :Type of the data (integer, float, Python object etc.)Size of the data (number of bytes)Byte order of the data (little-endian or
3 min read
Modify Numpy array to store an arbitrary length string NumPy builds on (and is a successor to) the successful Numeric array object. Its goal is to create the corner-stone for a useful environment for scientific computing. NumPy provides two fundamental objects: an N-dimensional array object (ndarray) and a universal function object (ufunc). The dtype of
4 min read
Python Lists VS Numpy Arrays Here, we will understand the difference between Python List and Python Numpy array. What is a Numpy array?NumPy is the fundamental package for scientific computing in Python. Numpy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operati
7 min read