Numpy char.encode() Function



The Numpy char.encode() function is used to encode each element in an array of strings into byte strings using a specified encoding such as UTF-8 or ASCII.

This function is useful when we need to convert regular strings into a specific encoded format typically for storage or transmission purposes.

Syntax

Following is the syntax of Numpy char.encode() function −

numpy.char.encode(a, encoding=None, errors=None)

Parameters

Below are the parameters of the Numpy char.encode() function −

  • a(array_like): The input array of strings to be encoded.

  • encoding(str, optional): The encoding used to convert the strings into bytes.. The default value is 'utf-8'.

  • errors(str, optional): This parameter specifies the error handling scheme where 'strict' raises an error, 'ignore' skips invalid characters and 'replace' replaces them with a placeholder.

Return Value

This function returns an array of byte strings with the same shape as the input array. Each element of the output array is a byte-encoded version of the corresponding string in the input array encoded using the specified encoding.

Example 1

Following is the basic example of Numpy char.encode() function. Here in this example we have an array of strings and we are encoding them into byte strings using UTF-8 encoding −

import numpy as np

arr = np.array(['hello', 'world', 'numPy'])
encoded_arr = np.char.encode(arr, encoding='utf-8')
print(encoded_arr)

Below is the output of the basic example of numpy.char.encode() function −

[b'hello' b'world' b'numPy']

Example 2

when we want to encode the strings using ASCII encoding then we can specify the encoding parameter. Below is the example of encoding the array strings with ASCII −

import numpy as np
arr = np.array(['hello', 'world', 'numPy'])
encoded_arr = np.char.encode(arr, encoding='ascii')
print(encoded_arr)

Here is the output of the above example −

[b'hello' b'world' b'numPy']

Example 3

when we want to replace invalid characters with a placeholder during encoding then we can use the errors='replace' parameter of the function char.encode().Here is the example of it −

import numpy as np
arr = np.array(['hello', 'wrld', 'numPy'])
encoded_arr = np.char.encode(arr, encoding='ascii', errors='replace')
print(encoded_arr)

Here is the output of replacing the invalid characters in the array strings −

[b'hello' b'w?rld' b'numPy']
numpy_string_functions.htm
Advertisements