What is the numpy.char.lstrip() function from NumPy in Python?

Overview

The char.lstrip() function in Python is used to remove the leading characters of each element of an array of strings.

Syntax

numpy.char.lstrip(a, chars = None)

Function parameters

The char.lstrip() function takes the following parameter values:

  • a: This represents the input array of strings or unicode.
  • chars: This represents the particular set of characters that need to be removed from the input array of strings. This parameter value is optional.

Return value

The char.lstrip() function returns an array of strings or Unicode, depending on the type of the input in the input array.

Code example

import numpy as np
# creating an array
myarray = np.array(["hello", "hey! ,", "hey yO"])
# implementing the char.lstrip() function to remove `eo`
print(np.char.lstrip(myarray, "he"))

Code Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array called myarray, using the array() function.
  • Line 7: We implement the char.lstrip() function on the array myarray by removing the starting set of characters, "he", from the element of the array. Then, we print the result to the console.

Free Resources