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

Overview

The char.equal() function in Python is used to check, one by one, if the elements of two arrays of the same shape are equal. It returns True if they are equal. Otherwise, it returns False.

Syntax

char.equal(x1, x2)

Parameter value

The char.equal() function takes two parameter values, x1 and x2, which are two input arrays of the same shape.

Return value

The char.equal() function returns an output array of bools.

Code example

import numpy as np
# creating input arrays
myarray1 = np.array(["Theo", "10", "Numpy", "YES", "1", "and"])
myarray2 = np.array(["Theo", "100", "numpy", "YES", "1", "and"])
# printing the arrays
print(myarray1)
print(myarray2)
# implementing the char.equal() function on both arrays to see if they are equal
print(np.char.equal(myarray1, myarray2))

Code explanation

  • Line 1: We import the numpy module.
  • Lines 4–5: We create the input arrays, myarray1 and myarray2, using the array() function.
  • Lines 8–9: We print the arrays myarray1 and myarray2.
  • Line 12: We invoke the char.equal() function on the arrays to check if all the elements on both the arrays are equal. Then, we print the result to the console.

Free Resources