Python numpy.char.mod() Method (With Examples)

By IncludeHelp Last updated : November 29, 2023

Python numpy.char.mod() Method

The mod() method is a built-in method of the char class in the numpy module, it is a pre-Python 2.6 string formatting (interpolation), element-wise for a pair of array-like strings or Unicode. It simply returns the result of (arr % i).

Module

The following module is required to use the mod() method:

import numpy as np

Syntax

The following is the syntax of the mod() method:

char.mod(arr, i)

Parameter(s)

The following are the parameter(s):

  • arr: The array-like value to be applied element-wise for interpolating into the string
  • i: An array-like value on which string formatting will be applied.

Return Value

The return type of the mod() method is <class 'numpy.ndarray'>, it returns an updated array after performing the element-wise string formatting.

Example 1: Applying single string formatting to all array values

# Importing numpy module
import numpy as np

# Creating single element array with
# string formatting character
arr1 = ["%d"]
# Creating an array of ints
i = [10, 20, 65, 7]

# Printing the values
print("arr1: ", arr1)
print("i: ", i)

# Using char.mod() method
result = np.char.mod(arr1, i)

# Printing the result
print("\nThe result is:\n", result)
print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr1:  ['%d']
i:  [10, 20, 65, 7]

The result is:
 ['10' '20' '65' '7']

The type of the result is: <class 'numpy.ndarray'>

Example 2: Applying multiple string formatting to all array values

# Importing numpy module
import numpy as np

# Creating array of string formatting
# characters
arr1 = ["%d", "%3d", "%5d", "%7d"]
# Creating an array of ints
i = [10, 20, 65, 7]

# Printing the values
print("arr1: ", arr1)
print("i: ", i)

# Using char.mod() method
result = np.char.mod(arr1, i)

# Printing the result
print("\nThe result is:\n", result)
print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr1:  ['%d', '%3d', '%5d', '%7d']
i:  [10, 20, 65, 7]

The result is:
 ['10' ' 20' '   65' '      7']

The type of the result is: <class 'numpy.ndarray'>

Example 3: Applying different string formatting to all array values

# Importing numpy module
import numpy as np

# Creating array of string formatting
# characters
arr1 = ["%d", "%f", "c", "%07d"]
# Creating an array of ints
i = [10, 20, 65, 7]

# Printing the values
print("arr1: ", arr1)
print("i: ", i)

# Using char.mod() method
result = np.char.mod(arr1, i)

# Printing the result
print("\nThe result is:\n", result)
print("\nThe type of the result is:", type(result))

Output

The output of the above example is:

arr1:  ['%d', '%f', 'c', '%07d']
i:  [10, 20, 65, 7]

The result is:
 ['10' '20.000000' 'c' '0000007']

The type of the result is: <class 'numpy.ndarray'>

Python NumPy Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.