- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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... <U8 Array Dimensions... 1 Our Array Shape... (5,) Number of elements in the Array... 5 Result (length element-wise)... [3 8 5 4 3]
- Related Articles
- Return the element-wise square of the array input in Python
- Return element-wise string multiple concatenation in Numpy
- Return element-wise string concatenation for two arrays of string in Numpy
- Return the element-wise square-root of a complex type array in Numpy
- Return the base 10 logarithm of the input array element-wise in Numpy
- Return the non-negative square-root of an array element-wise in Numpy
- Return element-wise title cased version of string or Unicode in Numpy
- Return the natural logarithm of one plus the input array element-wise in Numpy
- Return the element-wise remainder of division in Numpy
- Return the truth value of an array less than another element-wise in Numpy
- Return the truth value of an array equal to another element-wise in Numpy
- Return the truth value of an array greater than another element-wise in Numpy
- Return element-wise quotient and remainder simultaneously in Python Numpy
- Return the reciprocal of the argument element-wise in Numpy
- Return the truth value of an array not equal to another element-wise in Numpy

Advertisements