Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Return the length of a string array element-wise in Python
To return the length of a string array element-wise, use the numpy.char.str_len() method in Python Numpy. The method returns an output array of integers.
Steps
At first, import the required library −
import numpy as np
Create a One-Dimensional array of strings −
arr = np.array(['Amy', 'Scarlett', 'Katie', 'Brad', 'Tom'])
Displaying our array −
print("Array...\n",arr)
Get the datatype −
print("\nArray datatype...\n",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the Array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nNumber of elements in the Array...\n",arr.size)
To return the length of a string array element-wise, use the numpy.char.str_len() method −
print("\nResult (length element-wise)...\n",np.char.str_len(arr))
Example
import numpy as np
# Create a One-Dimensional array of strings
arr = np.array(['Amy', 'Scarlett', 'Katie', 'Brad', 'Tom'])
# Displaying our array
print("Array...\n",arr)
# Get the datatype
print("\nArray datatype...\n",arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)
# Get the number of elements of the Array
print("\nNumber of elements in the Array...\n",arr.size)
# To return the length of a string array element-wise, use the numpy.char.str_len() method in Python Numpy
# The method returns an output array of integers.
print("\nResult (length element-wise)...\n",np.char.str_len(arr))
Output
Array... ['Amy' 'Scarlett' 'Katie' 'Brad' 'Tom'] Array datatype...
Advertisements
