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

Overview

The char.mod() function in Python is simply used to return (a % i), which is pre-Python 2.6 string formatting, element-wise, for a pair of the array.

Syntax

char.mod(a, values)

Parameter value

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

  • a: This represents an array object of strings or Unicode.
  • values: This represents the values that will be interpolated element-wise into the string.

Return value

The char.mod() function returns an output array of strings or Unicode depending on the input type.

Code example

import numpy as np
# creating an array object
myarray = np.array([1, 10, 20])
# implememting the char.mode() function
print(np.char.mod('%d', myarray))

Code explanation

  • Line 1: We import the numpy module.
  • Line 3: We create an array object myarray.
  • Line 6: We implement the char.mod() function on the values of the array myarray and print our result.

Free Resources