What is the numpy.char.str_len() function in Python?

Overview

The char.str_len() function in Python is used to return the length element-wise. In other words, it is used to obtain the string length of each element of an array.

Syntax

char.str_len(a)
Syntax for the char.str_len() function

Parameter

The char.str_len() function takes a single parameter value, a, which represents the input array of strings.

Return value

The char.str_len() function returns an output array of integers.

Example

Let's look at the code below:

import numpy as np
# creating the input array
myarray = np.array(["Theo", "10", "Numpy", "YES", "1", "and"])
# printing the input array
print(myarray)
# implementing the char.str_len() function
print(np.char.str_len(myarray))

Explanation

  • Line 1: We import the numpy module.
  • Line 3: We create an input array of strings, myarray, using the array() function.
  • Line 6: We print the input array, myarray.
  • Line 9: We implement the char.str_len() function on the input array. We obtain and print the result to the console.

Free Resources